Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 934
Jengerer Avatar asked Nov 03 '10 02:11

Jengerer


2 Answers

What you're seeing is undefined behavior—it's not actually a memory leak. The memory for the C string has been deallocated (at least as far as you're concerned), but the data there is still technically accessible. When you deallocate memory, the memory usually doesn't get erased, so the data there often stays around so long as the memory doesn't get reused by a subsequent allocation.

Reading data after it's been deallocated is undefined behavior: you might get what the data was before it was deallocated, you might get garbage data, you might crash your program, or you could even erase your hard drive (although that's not very likely).

So long as the std::string object is being properly deallocated, then any memory used for its C string representation will also be deallocated. You don't need to worry about that.


EDIT: Actually it turns out your object wasn't getting fulled destroyed because the parent class Popup didn't have a virtual destructor. As a result, the destructor for the subclass Dialog wasn't getting called, so the destructor for the std::string instance wasn't getting called.

like image 100
Adam Rosenfield Avatar answered Nov 12 '22 18:11

Adam Rosenfield


The problem is most likely not in std::string, but in obj (whatever type that is). Note that you deleted obj, not cppString. My guess is that obj does not store objString in a smart pointer class nor does it delete objString in its destructor, and hence you have that leak.

like image 3
Michael Aaron Safyan Avatar answered Nov 12 '22 17:11

Michael Aaron Safyan