Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to update the string value from one class to another class of Property page in mfc.I am getting an runtime error

How to update the string value from one class to another class of Property page in mfc.I am getting an runtime error

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialc++helpannouncement
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11403697
    wrote on last edited by
    #1

    Below is my code tried class CVersion void CVersion::ReadValue(BYTE *Buffer, int n_Length, int n_Source) { EnterCriticalSection(&m_sReadCritical); CString tempBuffer((BYTE*)Buffer); m_svalue = tempBuffer; m_cData->Data(this); LeaveCriticalSection(&m_sReadCritical); } class CClassVersion void CClassVersion::Data(CVersion *pcVers) { CString m_Value1 = pcVers->m_sValue; this->SetDlgItemTextA(IDC_EDIT_VAL,m_Value1); } //Declared in CVersion header file protected: CClassVersion* m_cData; An Runtime error occurs"Unhandled exception at 0x782ac7da in .exe:0xC0000005:Access violation reading location 0x00000020." after SetDlgItemText stmt. Please guide me for the same.

    L F J 3 Replies Last reply
    0
    • U User 11403697

      Below is my code tried class CVersion void CVersion::ReadValue(BYTE *Buffer, int n_Length, int n_Source) { EnterCriticalSection(&m_sReadCritical); CString tempBuffer((BYTE*)Buffer); m_svalue = tempBuffer; m_cData->Data(this); LeaveCriticalSection(&m_sReadCritical); } class CClassVersion void CClassVersion::Data(CVersion *pcVers) { CString m_Value1 = pcVers->m_sValue; this->SetDlgItemTextA(IDC_EDIT_VAL,m_Value1); } //Declared in CVersion header file protected: CClassVersion* m_cData; An Runtime error occurs"Unhandled exception at 0x782ac7da in .exe:0xC0000005:Access violation reading location 0x00000020." after SetDlgItemText stmt. Please guide me for the same.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You should step through the code with your debugger to see which variable is invalid. There is nothing obvious in the above code, although it is not easy to understand what it is supposed to be doing.

      U 1 Reply Last reply
      0
      • U User 11403697

        Below is my code tried class CVersion void CVersion::ReadValue(BYTE *Buffer, int n_Length, int n_Source) { EnterCriticalSection(&m_sReadCritical); CString tempBuffer((BYTE*)Buffer); m_svalue = tempBuffer; m_cData->Data(this); LeaveCriticalSection(&m_sReadCritical); } class CClassVersion void CClassVersion::Data(CVersion *pcVers) { CString m_Value1 = pcVers->m_sValue; this->SetDlgItemTextA(IDC_EDIT_VAL,m_Value1); } //Declared in CVersion header file protected: CClassVersion* m_cData; An Runtime error occurs"Unhandled exception at 0x782ac7da in .exe:0xC0000005:Access violation reading location 0x00000020." after SetDlgItemText stmt. Please guide me for the same.

        F Offline
        F Offline
        Freak30
        wrote on last edited by
        #3

        I assume this is a threading problem. The code can probably be called from different threads (that's why you use a Critical Section), but the control text can only be set from the GUI thread. The easiest way to achieve this is using a timer. So in the ReadValue() fucntion you only put the data into m_svalue. And in the OnTimer() function (which runs in GUI thread) you call Data(). This would of cource mean that you need to store the most recently used CClassVersion object somewhere.

        The good thing about pessimism is, that you are always either right or pleasently surprised.

        U 1 Reply Last reply
        0
        • U User 11403697

          Below is my code tried class CVersion void CVersion::ReadValue(BYTE *Buffer, int n_Length, int n_Source) { EnterCriticalSection(&m_sReadCritical); CString tempBuffer((BYTE*)Buffer); m_svalue = tempBuffer; m_cData->Data(this); LeaveCriticalSection(&m_sReadCritical); } class CClassVersion void CClassVersion::Data(CVersion *pcVers) { CString m_Value1 = pcVers->m_sValue; this->SetDlgItemTextA(IDC_EDIT_VAL,m_Value1); } //Declared in CVersion header file protected: CClassVersion* m_cData; An Runtime error occurs"Unhandled exception at 0x782ac7da in .exe:0xC0000005:Access violation reading location 0x00000020." after SetDlgItemText stmt. Please guide me for the same.

          J Online
          J Online
          jeron1
          wrote on last edited by
          #4

          How is m_cData initialized?

          "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst

          U 1 Reply Last reply
          0
          • J jeron1

            How is m_cData initialized?

            "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst

            U Offline
            U Offline
            User 11403697
            wrote on last edited by
            #5

            m_cData is declared as pointer

            J 1 Reply Last reply
            0
            • L Lost User

              You should step through the code with your debugger to see which variable is invalid. There is nothing obvious in the above code, although it is not easy to understand what it is supposed to be doing.

              U Offline
              U Offline
              User 11403697
              wrote on last edited by
              #6

              Can i use callback function over der?How??????please guide me for the same.Thankyou

              L 1 Reply Last reply
              0
              • U User 11403697

                Can i use callback function over der?How??????please guide me for the same.Thankyou

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Member 11438021 wrote:

                Can i use callback function

                For what reason? You first need to diagnose what is happening in your code to cause the error. When you enter the critical section what are you trying to synchronise with? [edit]

                void CVersion::ReadValue(BYTE *Buffer, int n_Length, int n_Source)
                {

                EnterCriticalSection(&m_sReadCritical);

                CString tempBuffer((BYTE*)Buffer);
                m_svalue = tempBuffer;
                m_cData->Data(this);

                LeaveCriticalSection(&m_sReadCritical);
                }

                You are setting m_svalue to point to tempBuffer, but as soon as you exit this function that buffer will get released so the pointer is no longer valid. Looking at this code again I cannot see what useful purpose it serves. [/edit]

                U 1 Reply Last reply
                0
                • F Freak30

                  I assume this is a threading problem. The code can probably be called from different threads (that's why you use a Critical Section), but the control text can only be set from the GUI thread. The easiest way to achieve this is using a timer. So in the ReadValue() fucntion you only put the data into m_svalue. And in the OnTimer() function (which runs in GUI thread) you call Data(). This would of cource mean that you need to store the most recently used CClassVersion object somewhere.

                  The good thing about pessimism is, that you are always either right or pleasently surprised.

                  U Offline
                  U Offline
                  User 11403697
                  wrote on last edited by
                  #8

                  Can i use Handler or callback function,if yes.How??? Guide for the same.Ty.

                  1 Reply Last reply
                  0
                  • L Lost User

                    Member 11438021 wrote:

                    Can i use callback function

                    For what reason? You first need to diagnose what is happening in your code to cause the error. When you enter the critical section what are you trying to synchronise with? [edit]

                    void CVersion::ReadValue(BYTE *Buffer, int n_Length, int n_Source)
                    {

                    EnterCriticalSection(&m_sReadCritical);

                    CString tempBuffer((BYTE*)Buffer);
                    m_svalue = tempBuffer;
                    m_cData->Data(this);

                    LeaveCriticalSection(&m_sReadCritical);
                    }

                    You are setting m_svalue to point to tempBuffer, but as soon as you exit this function that buffer will get released so the pointer is no longer valid. Looking at this code again I cannot see what useful purpose it serves. [/edit]

                    U Offline
                    U Offline
                    User 11403697
                    wrote on last edited by
                    #9

                    I want to update the text(string) on u/i page.I am sending the message on button click and want to receive the same string on Edit controlBox.

                    L 1 Reply Last reply
                    0
                    • U User 11403697

                      I want to update the text(string) on u/i page.I am sending the message on button click and want to receive the same string on Edit controlBox.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Then you need to manage the lifetime of the string. If you pass it from one thread to another then you must ensure it is not deleted from its source until the destination has taken a secure copy. In your sample code your destination thread is taking a copy, but only into a temporary location which immeditely gets disposed. Assuming that m_svalue is a string pointer then you just need to allocate a buffer of the required size to it, and copy the message string across. You can then delete it at some later point.

                      U 1 Reply Last reply
                      0
                      • U User 11403697

                        m_cData is declared as pointer

                        J Online
                        J Online
                        jeron1
                        wrote on last edited by
                        #11

                        I understand that, but what value are you assigning to it?

                        "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst

                        1 Reply Last reply
                        0
                        • L Lost User

                          Then you need to manage the lifetime of the string. If you pass it from one thread to another then you must ensure it is not deleted from its source until the destination has taken a secure copy. In your sample code your destination thread is taking a copy, but only into a temporary location which immeditely gets disposed. Assuming that m_svalue is a string pointer then you just need to allocate a buffer of the required size to it, and copy the message string across. You can then delete it at some later point.

                          U Offline
                          U Offline
                          User 11403697
                          wrote on last edited by
                          #12

                          Can you just give the sample code for the same.Because i tried ,but i am doing a small mistake somewhere.How will i store the string in a buffer so that it can be accessible from another class and use for updating the text.

                          L 1 Reply Last reply
                          0
                          • U User 11403697

                            Can you just give the sample code for the same.Because i tried ,but i am doing a small mistake somewhere.How will i store the string in a buffer so that it can be accessible from another class and use for updating the text.

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #13

                            Sample code for what? Allocate a memory buffer, copy the string into it, and pass it to the receiver. The receiver can then keep that buffer until it is no longer needed.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups