Help needed in MFC
-
Hello, I have a problem where I want to update two varibales in my View class through a dialog class CModalmodeDlg. When I press the "OK" button in the dialog I want two separate const char * members to be updated. The first case is when I select a Cstring from a dropdown and sends it to a const char * member (m_Vectorname) in the view class. That works great,but It doesnt work for the secondview member (m_Modefreq). When a try to display m_Modefreq in the view class it gets all corrupted. But it gets it right when I display it in a messagebox through the dialog. example. m_Modefreq in messagebox= "45.23 Hz" m_Modefreq in ViewClass = "^''*´" Another observation is that when I in the dialogs OnBnClickedOk() set the second variable (m_Modefreq) to always be the same static value it displays it correct in the view. below is a code snibbet, [code] void CModalmodeDlg::OnBnClickedOk() { //get the active view CFrameWnd* wnd = (CFrameWnd*) AfxGetMainWnd(); if(wnd) m_pView = (CVtkSDIView*) wnd->GetActiveView(); //get the selected string from the dropdown. CString str; m_DropDown.GetLBText( idx,str); const TCHAR *ptr; ptr = str; //update the first view member const char *m_Vectorname //works fine m_pView->m_Vectorname=(LPCTSTR)str; int idx = m_DropDown2.GetCurSel(); m_pView->m_Modeindex=idx; //the method ReturnMode returns a string depending on which index is selected //from the DropDown. Currentstring =m_pView->ReturnMode(idx); //Here I want the other const char * member (m_Modefreq) to be updated. //it prints out the right in a messagebox but as soon as I try to display it in the view //class it gets all corrupted. m_pView->m_Modefreq=Currentstring.c_str(); [/code] When I set it to a static value [code] like this: m_pView->m_Modefreq="45.2 Hz" it manage to update it right. [/code] I dont really know what to do about this, please letme know if you have any ideas! Regards Peter
-
Hello, I have a problem where I want to update two varibales in my View class through a dialog class CModalmodeDlg. When I press the "OK" button in the dialog I want two separate const char * members to be updated. The first case is when I select a Cstring from a dropdown and sends it to a const char * member (m_Vectorname) in the view class. That works great,but It doesnt work for the secondview member (m_Modefreq). When a try to display m_Modefreq in the view class it gets all corrupted. But it gets it right when I display it in a messagebox through the dialog. example. m_Modefreq in messagebox= "45.23 Hz" m_Modefreq in ViewClass = "^''*´" Another observation is that when I in the dialogs OnBnClickedOk() set the second variable (m_Modefreq) to always be the same static value it displays it correct in the view. below is a code snibbet, [code] void CModalmodeDlg::OnBnClickedOk() { //get the active view CFrameWnd* wnd = (CFrameWnd*) AfxGetMainWnd(); if(wnd) m_pView = (CVtkSDIView*) wnd->GetActiveView(); //get the selected string from the dropdown. CString str; m_DropDown.GetLBText( idx,str); const TCHAR *ptr; ptr = str; //update the first view member const char *m_Vectorname //works fine m_pView->m_Vectorname=(LPCTSTR)str; int idx = m_DropDown2.GetCurSel(); m_pView->m_Modeindex=idx; //the method ReturnMode returns a string depending on which index is selected //from the DropDown. Currentstring =m_pView->ReturnMode(idx); //Here I want the other const char * member (m_Modefreq) to be updated. //it prints out the right in a messagebox but as soon as I try to display it in the view //class it gets all corrupted. m_pView->m_Modefreq=Currentstring.c_str(); [/code] When I set it to a static value [code] like this: m_pView->m_Modefreq="45.2 Hz" it manage to update it right. [/code] I dont really know what to do about this, please letme know if you have any ideas! Regards Peter
m_pView->m_Modefreq=Currentstring.c_str(); This is the error. Currentstring is dialog local std::string. You are just doing a shallow copy of its internal pointer. When the dialog ends, the strings destructor destroys its contents. You need a deep copy. If you use a std::string or a CString in your view class, it will be ok. Otherwise you'll have to do the memory management yourself with new, strcpy and delete, which also works.
-
Hello, I have a problem where I want to update two varibales in my View class through a dialog class CModalmodeDlg. When I press the "OK" button in the dialog I want two separate const char * members to be updated. The first case is when I select a Cstring from a dropdown and sends it to a const char * member (m_Vectorname) in the view class. That works great,but It doesnt work for the secondview member (m_Modefreq). When a try to display m_Modefreq in the view class it gets all corrupted. But it gets it right when I display it in a messagebox through the dialog. example. m_Modefreq in messagebox= "45.23 Hz" m_Modefreq in ViewClass = "^''*´" Another observation is that when I in the dialogs OnBnClickedOk() set the second variable (m_Modefreq) to always be the same static value it displays it correct in the view. below is a code snibbet, [code] void CModalmodeDlg::OnBnClickedOk() { //get the active view CFrameWnd* wnd = (CFrameWnd*) AfxGetMainWnd(); if(wnd) m_pView = (CVtkSDIView*) wnd->GetActiveView(); //get the selected string from the dropdown. CString str; m_DropDown.GetLBText( idx,str); const TCHAR *ptr; ptr = str; //update the first view member const char *m_Vectorname //works fine m_pView->m_Vectorname=(LPCTSTR)str; int idx = m_DropDown2.GetCurSel(); m_pView->m_Modeindex=idx; //the method ReturnMode returns a string depending on which index is selected //from the DropDown. Currentstring =m_pView->ReturnMode(idx); //Here I want the other const char * member (m_Modefreq) to be updated. //it prints out the right in a messagebox but as soon as I try to display it in the view //class it gets all corrupted. m_pView->m_Modefreq=Currentstring.c_str(); [/code] When I set it to a static value [code] like this: m_pView->m_Modefreq="45.2 Hz" it manage to update it right. [/code] I dont really know what to do about this, please letme know if you have any ideas! Regards Peter
The problem most likely lies here:
m_pView->m_Modefreq=Currentstring.c_str();
You are storing a pointer to the internal contents of
Currentstring
intom_pView->m_Modefreq
, but this internal contents can change (or be deleted) as soon asCurrentstring
is modified or destroyed. In your snippet it is not clear whetherCurrentstring
is a local variable or not: if the former, then thechar
buffer pointed to bym_Modefreq
will be destroyed as soon asCModalmodeDlg::OnBnClickedOk
exists. As a general rule of thumb, never store the pointer returned bystd::string::c_string
. To solve your particular problem, change the type ofm_Modefreq
to astd::string
. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]! -
The problem most likely lies here:
m_pView->m_Modefreq=Currentstring.c_str();
You are storing a pointer to the internal contents of
Currentstring
intom_pView->m_Modefreq
, but this internal contents can change (or be deleted) as soon asCurrentstring
is modified or destroyed. In your snippet it is not clear whetherCurrentstring
is a local variable or not: if the former, then thechar
buffer pointed to bym_Modefreq
will be destroyed as soon asCModalmodeDlg::OnBnClickedOk
exists. As a general rule of thumb, never store the pointer returned bystd::string::c_string
. To solve your particular problem, change the type ofm_Modefreq
to astd::string
. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!Hi, Thanks for your quick replys, to bad that I didn't notice until now:) I will try both of the tips that has been suggested. Niklas you suggested that I should do a deepcopy, I suppose you mean create a new, strcpy, and then pass it, then delete it. I realized that the buffer that the m_Modefreq points to will be destroyed but how come the other passing of data works.If you remember from my first thread I converted an CString str and passed it to a const char * m_Vectorname. Shouldn't that pointer also be corrupted as the other one since it points to a buffer that is being destroyed when it exists the metohd? //get the selected string from the dropdown. CString str; m_DropDown.GetLBText( idx,str); const TCHAR *ptr; ptr = str; //update the first view member const char *m_Vectorname //works fine m_pView->m_Vectorname=(LPCTSTR)str;
-
Hi, Thanks for your quick replys, to bad that I didn't notice until now:) I will try both of the tips that has been suggested. Niklas you suggested that I should do a deepcopy, I suppose you mean create a new, strcpy, and then pass it, then delete it. I realized that the buffer that the m_Modefreq points to will be destroyed but how come the other passing of data works.If you remember from my first thread I converted an CString str and passed it to a const char * m_Vectorname. Shouldn't that pointer also be corrupted as the other one since it points to a buffer that is being destroyed when it exists the metohd? //get the selected string from the dropdown. CString str; m_DropDown.GetLBText( idx,str); const TCHAR *ptr; ptr = str; //update the first view member const char *m_Vectorname //works fine m_pView->m_Vectorname=(LPCTSTR)str;
Shouldn't that pointer also be corrupted as the other one since it points to a buffer that is being destroyed when it exists the metohd? Yep... seems to me you're just being lucky here; the memory pointed to by
m_pView->m_Vectorname
is free memory, and sooner or later it'll get corrupted. I suggest you also changem_pView->m_Vectorname
to bestd::string
. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!