C/C++ string memory leaks?

I'm using the STL string within an application of mine, and I was recently testing it for memory leaks, and I noticed that a lot of my strings weren't being properly deallocated by the end of the program.

I tested the following code (not verbatim) with one of the strings:

const string* cppString = &obj->objString;
const char* cString = cppString->c_str();
delete obj;

After that, I put a break-point and noticed that, while the string that cppString pointed to no longer existed, cString was still pointing to a C-style string, which surely enough, was the one that failed to be deallocated at the end.

Am I missing something in terms of how C/C++ strings work? How can I get the C representation of the string to be deallocated as well?

EDIT: Some more information. My obj class is of type Dialog, which inherits Popup. I thought that might've been it, since when I delete obj, I'm treating it as a Popup*, but I tried it in a small separate program, and deleting as a parent class properly removes the child member variables (which makes sense, of course).

I used the memory leak tracing within VS, and it shows that the string that ended up leaking was the one that was created when I made the Dialog and set the objString to the string passed as a reference to the constructor.

Thanks,
Jengerer

8
задан Jengerer 3 November 2010 в 04:35
поделиться