Question

Unfortunately the documentation I have is either (a) original product documentation without any errrata (MS VC++ 6.0 help files) or (b) later MSDN help that applies to later MFC versions.

In particular:

[Q1] Is the operator += known to be buggy in VC++6 MFC CString? This code from VC++6 had to be fixed before it would even compile in a modern MFC app:

    CString szTemp;
    unsigned char m_chReceive[MY_BUF_SIZE];

    // compiles and seems to run but may be buggy in VC++6, won't compile in modern MFC
szTemp += m_chReceive; 
    // the above won't compile in modern MFC versions, but this "&+cast" does:
szTemp.Append( (const char *)&m_chReceive[0]); 

[Q2] Is it safe and robust to return a CString as the result of a function, in this manner, or does this cause memory corruption?

 CString MyClass:MyMethod(void)
 {   CString Stuff;
     // set a value to Stuff here.
     return Stuff; // return a stack-allocated-CString 
 }

I have code that uses the above two things all over the place and which also seems to exhibit random runtime memory corruption. Those two things are red flags to me, am I right in suspecting that CString was intended by the authors of MFC in Visual C++ 6.0 to be nice simple thing that you could use like it was an int or a char type, and return it from a function and somehow copy constructors and memory management all just worked?

Obvious stuff: Yes of course I will get all my code off VC++ 6.0 when I can, but I first need to patch a production system which is crashing, and then I can begin the huge task of moving this legacy codebase forward.

Was it helpful?

Solution

According to Documentation for VC6.0

CString objects can grow as a result of concatenation operations. CString objects follow “value semantics.”

OTHER TIPS

Microsoft Documentation seems to indicate that CString in purpose is similar to std::string in that it grows automatically when needed, and can be safely be passed around as arguements or return values of functions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top