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. Help needed in MFC

Help needed in MFC

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++databasetutorialannouncement
5 Posts 3 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.
  • I Offline
    I Offline
    Ibana
    wrote on last edited by
    #1

    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

    N J 2 Replies Last reply
    0
    • I Ibana

      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

      N Offline
      N Offline
      Niklas L
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • I Ibana

        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

        J Offline
        J Offline
        Joaquin M Lopez Munoz
        wrote on last edited by
        #3

        The problem most likely lies here:

        m_pView->m_Modefreq=Currentstring.c_str();

        You are storing a pointer to the internal contents of Currentstring into m_pView->m_Modefreq, but this internal contents can change (or be deleted) as soon as Currentstring is modified or destroyed. In your snippet it is not clear whether Currentstring is a local variable or not: if the former, then the char buffer pointed to by m_Modefreq will be destroyed as soon as CModalmodeDlg::OnBnClickedOk exists. As a general rule of thumb, never store the pointer returned by std::string::c_string. To solve your particular problem, change the type of m_Modefreq to a std::string. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!

        I 1 Reply Last reply
        0
        • J Joaquin M Lopez Munoz

          The problem most likely lies here:

          m_pView->m_Modefreq=Currentstring.c_str();

          You are storing a pointer to the internal contents of Currentstring into m_pView->m_Modefreq, but this internal contents can change (or be deleted) as soon as Currentstring is modified or destroyed. In your snippet it is not clear whether Currentstring is a local variable or not: if the former, then the char buffer pointed to by m_Modefreq will be destroyed as soon as CModalmodeDlg::OnBnClickedOk exists. As a general rule of thumb, never store the pointer returned by std::string::c_string. To solve your particular problem, change the type of m_Modefreq to a std::string. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!

          I Offline
          I Offline
          Ibana
          wrote on last edited by
          #4

          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;

          J 1 Reply Last reply
          0
          • I Ibana

            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;

            J Offline
            J Offline
            Joaquin M Lopez Munoz
            wrote on last edited by
            #5

            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 change m_pView->m_Vectorname to be std::string. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!

            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