CString access violations!
Hi,
I'm having access violation problems with CString. Checking the boards, I discovered that I do not suffer alone. A solution recommended was to type cast to LPCTSTR before assigning the new CString value.
I'm still getting access violation problems, though in a new spot. It now appears in the red-line in the following code.
void CString::AssignCopy(int nSrcLen, LPCTSTR lpszSrcData)
{
AllocBeforeWrite(nSrcLen);
memcpy(m_pchData, lpszSrcData, nSrcLen*sizeof(TCHAR));
GetData()->nDataLength = nSrcLen;
m_pchData[nSrcLen] = '\0';
}
The strange thing is, that both before and after the memcpy, the m_pchData points to garbage. I did initialize it.
Hope this info isn't too vague. Hope someone else has suffered through similar problems and has an idea.
Mat.
Re: CString access violations!
Quote:
Originally posted by Mat C
Hi,
I'm having access violation problems with CString. Checking the boards, I discovered that I do not suffer alone. A solution recommended was to type cast to LPCTSTR before assigning the new CString value.
I'm still getting access violation problems, though in a new spot. It now appears in the red-line in the following code.
void CString::AssignCopy(int nSrcLen, LPCTSTR lpszSrcData)
{
AllocBeforeWrite(nSrcLen);
memcpy(m_pchData, lpszSrcData, nSrcLen*sizeof(TCHAR));
GetData()->nDataLength = nSrcLen;
m_pchData[nSrcLen] = '\0';
}
The strange thing is, that both before and after the memcpy, the m_pchData points to garbage. I did initialize it.
Hope this info isn't too vague. Hope someone else has suffered through similar problems and has an idea.
Mat.
This doesn't mean there is a problem with CString. What you showed is just a symptom to a problem that may not have anything to do with CString. You can screw up any object if your coding is faulty, i.e. overwrite memory, calling delete twice on the same dynamic object, whatever. No class is invulnerable to coding that has memory bugs, faulty ccoding, or use undefined behavior. I highly doubt that if you are using CString correctly, CString would have a bug.
You need to show exactly how you are manipulating these CString objects in your program. Also, it would be helpful if you can mimic, with a very small, compilable example, of what you are doing with the CStrings and see if you can duplicate the error. There are too many posts that claim that CString has these bugs but always all of these problems have been found to be other aspects of the program that is affecting the CStrings. and not bugs with CString.
Regards,
Paul McKenzie
CString wins... can I be sloppy with CMap?
Well, trying to reproduce the bug led me to the error of my ways.
Basically, the problem was caused by a little function like this one:
CNewInfo* CTheDocument::GetNewInfo()
{
CDataSet oDataSet;
m_DataMap.Lookup(m_strCurKey, oDataSet);
return &oDataSet.m_NewInfo;
}
As is now obviously the problem, oDataSet goes out of scope at the end of this function, and the pointer in the calling method is left pointing to an address which is no longer valid. What threw me for a loop was that I could manipulate the integer members of CNewInfo from the pointer in the calling function. Since re-assigning the CString member caused me grief, I automatically assumed he was the problem.
What I was trying to do by this function was to get a reference directly to the CNewInfo object in the DataMap, so that I wouldn't have to do a SetAt() afterwards. But it seems that CMap is too tight of a class to let me be sloppy with it.
Out of curiosity though, would anyone know if it is possible to return a pointer to a members of a class using CMap as a template?
Thanks for the help. Maybe one day I'll be able to give advice too.
Mat.
Re: CString access violations!
Quote:
Originally posted by Mat C
Hope this info isn't too vague. Hope someone else has suffered through similar problems and has an idea.
I hope you don't mind me commenting on one thing that I think was not clear. The code you posted appears to me to be MFC source code, not your source code. However that was not made clear.