PVS studio found a potential defect which I am investigating; warning V623 http://www.viva64.com/en/d/0240/
However, not enough is explained to understand why the temporary copy is being made. If CString::operator wchar_t*() is defined like:
wchar_t* newbuf = new wchar_t[20];
// do things to put the current CString value in there
return newbuf;
Then there's a memory leak, but no pointer to a destroyed temporary. I see only one other possible implementation, which is to return an already existing wchar_t* pointer contained in the CString, which would not make a new temporary. Something like this:
return m_pContents;
So, two questions; where can I find the actual implementation for CString? And, what valid implementation of this function (on any selfmade type) could have the conversion operator return a pointer to a destroyed temporary? I just don't believe that Microsfoot would implement it like:
CString tempCopy(*this);
return tempCopy.m_pContents;
In your linked PVS studio code, you have a ternary operator taking a wchar_t *
and a CString
. Your problem is not with the conversion operator, but that the ternary is promoting the wchar_t *
into a CString
. You either need to assign the result to a new CString
or make sure the ternary arguments are all the same type so that you get a reference back.