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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Why this is different? (CString copying over pszTitle)...

Why this is different? (CString copying over pszTitle)...

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresperformancequestion
4 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.
  • J Offline
    J Offline
    Joan M
    wrote on last edited by
    #1

    I have an array of property pages and I change their caption/title dynamically... This works in the commented way, but when I try to do it using the non commented way, it crashes... (I'd like to know why)... PREMISSES: THE CASE THAT WORKS: I have an array of property pages. I have an array of titles. Those two are members of the property sheet class. THE CASE THAT DON'T WORKS: I have an array of property pages. I have one CString var declared inside the cosntructor of the property sheet class that will be used to store all the titles in the property pages... THE CODE

    {
    ...

    this->m_ArrPPDlgParametritzacions[iNumPPags].m_psp.dwFlags |= PSP_USETITLE; // NOTHING TO DO...
    this->m_ArrPPDlgParametritzacions[iNumPPags].m_psp.dwFlags &= ~PSP_HASHELP; // NOTHING TO DO...

    //m_ArrTitolsPropertyPages[iNumPPags].Format("%s",((char *)pxmlNode->attributes->getNamedItem("EtiquetaDescriptiva")->Gettext())); // THIS ONE WORKS OK...

    csTitolPPag = ((char *)pxmlNode->attributes->getNamedItem("EtiquetaDescriptiva")->Gettext()); // THIS ONE WILL FAIL...

    this->m_ArrPPDlgParametritzacions[iNumPPags].m_psp.pszTitle = csTitolPPag;//m_ArrTitolsPropertyPages[iNumPPags]; // IF I USE THE COMMENTED CODE EVERYTHING IS ALLRIGHT, BUT IF I USE THE ONE THAT's NOT COMMENTED, THEN THE TITLES ARE STRANGE (SOMETHING LIKE THIS:"@#~|@#€|~€|~|"...)

    this->AddPage(&m_ArrPPDlgParametritzacions[iNumPPags]); // NOTHING TO DO...

    iNumPPags++; // NOTHING TO DO...
    }

    Any idea? It's not a vital thing, I can live having an array of CStrings using memory, but it would be better to be able to do it without the array... thank you in advance...

    https://www.robotecnik.com freelance robots, PLC and CNC programmer.

    T 1 Reply Last reply
    0
    • J Joan M

      I have an array of property pages and I change their caption/title dynamically... This works in the commented way, but when I try to do it using the non commented way, it crashes... (I'd like to know why)... PREMISSES: THE CASE THAT WORKS: I have an array of property pages. I have an array of titles. Those two are members of the property sheet class. THE CASE THAT DON'T WORKS: I have an array of property pages. I have one CString var declared inside the cosntructor of the property sheet class that will be used to store all the titles in the property pages... THE CODE

      {
      ...

      this->m_ArrPPDlgParametritzacions[iNumPPags].m_psp.dwFlags |= PSP_USETITLE; // NOTHING TO DO...
      this->m_ArrPPDlgParametritzacions[iNumPPags].m_psp.dwFlags &= ~PSP_HASHELP; // NOTHING TO DO...

      //m_ArrTitolsPropertyPages[iNumPPags].Format("%s",((char *)pxmlNode->attributes->getNamedItem("EtiquetaDescriptiva")->Gettext())); // THIS ONE WORKS OK...

      csTitolPPag = ((char *)pxmlNode->attributes->getNamedItem("EtiquetaDescriptiva")->Gettext()); // THIS ONE WILL FAIL...

      this->m_ArrPPDlgParametritzacions[iNumPPags].m_psp.pszTitle = csTitolPPag;//m_ArrTitolsPropertyPages[iNumPPags]; // IF I USE THE COMMENTED CODE EVERYTHING IS ALLRIGHT, BUT IF I USE THE ONE THAT's NOT COMMENTED, THEN THE TITLES ARE STRANGE (SOMETHING LIKE THIS:"@#~|@#€|~€|~|"...)

      this->AddPage(&m_ArrPPDlgParametritzacions[iNumPPags]); // NOTHING TO DO...

      iNumPPags++; // NOTHING TO DO...
      }

      Any idea? It's not a vital thing, I can live having an array of CStrings using memory, but it would be better to be able to do it without the array... thank you in advance...

      T Offline
      T Offline
      Tim Smith
      wrote on last edited by
      #2

      I would imagine that csTitolPPag is just a local string variable. The problem you are having is that this->m_ArrPPDlgParametritzacions[iNumPPags].m_psp.pszTitle is nothing more than a pointer to a character string. So when csTitolPPag goes out of scope, the string is deleted and thus pszTitle now points to trash. Tim Smith I'm going to patent thought. I have yet to see any prior art.

      J 1 Reply Last reply
      0
      • T Tim Smith

        I would imagine that csTitolPPag is just a local string variable. The problem you are having is that this->m_ArrPPDlgParametritzacions[iNumPPags].m_psp.pszTitle is nothing more than a pointer to a character string. So when csTitolPPag goes out of scope, the string is deleted and thus pszTitle now points to trash. Tim Smith I'm going to patent thought. I have yet to see any prior art.

        J Offline
        J Offline
        Joan M
        wrote on last edited by
        #3

        This is what I think, but is there any way to do it assigning the content, and not the direction? Thank you in advance...

        https://www.robotecnik.com freelance robots, PLC and CNC programmer.

        D 1 Reply Last reply
        0
        • J Joan M

          This is what I think, but is there any way to do it assigning the content, and not the direction? Thank you in advance...

          D Offline
          D Offline
          David Spain
          wrote on last edited by
          #4

          You would have to find the length of the string you are copying, then allocate a character array on the heap with length + 1 size, do a memset to set this new memory to 0, do a memcpy from the CString's psz pointer to this new memory, then set the array psz to be the new pointer. That way the memory will always be there in the array psz. Just be sure to do a delete [] on this array psz when this array is no longer needed, or will get a memory leak. (The memset to 0 may not be needed, it may be done automatically when call new.) David Spain

          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