How to update the string value from one class to another class of Property page in mfc.I am getting an runtime error
-
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.
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.
-
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.
-
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
m_cData is declared as pointer
-
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.
Can i use callback function over der?How??????please guide me for the same.Thankyou
-
Can i use callback function over der?How??????please guide me for the same.Thankyou
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 totempBuffer
, 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] -
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.
Can i use Handler or callback function,if yes.How??? Guide for the same.Ty.
-
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 totempBuffer
, 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]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.
-
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.
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. -
m_cData is declared as pointer
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
-
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.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.
-
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.