Why this is different? (CString copying over pszTitle)...
-
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...
-
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...
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.
-
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.
-
This is what I think, but is there any way to do it assigning the content, and not the direction? Thank you in advance...
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