Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trapping this Memory Leak 1

Status
Not open for further replies.

Kalisto

Programmer
Feb 18, 2003
997
GB
When I run my program and stop it, I have a few memory leaks. I have identified that one is down to the fact that I am using a singleton class, and there is no way I can stop from losing that memory, is there ? (but as it is when the program terminates, it is returned to the OS anyway (is this assumption correct ?)

One of my other classes contains an std::string member. When that classes destructor is called, I am clearing the string, but as it isn't a pointer to a string I cannot just delete it, and I seem to be losing 28 bytes here as well. (Is this just a case of making the class member be a pointer to a string, and then setting it in the constructor ?)

Thanks for any input, as this program is almost complete, but I want to eliminate these 2 leaks before I will be happy to let a 3rd party test it.

Cheers,
K
 
>singleton class, and there is no way I can stop from losing that memory, is there

Sure there is. Just give it a static destroyInstance method (that deletes the instance) that you call just before your app exits.

>but as it is when the program terminates, it is returned to the OS anyway (is this assumption correct ?)

Assumption is correct. However, that does not imply you shouldn't destruct your stuff properly.

>One of my other classes contains an std::string member...

When the std::string's destructor is called it will clean itself up, and you wouldn't have to worry about it. If you have a pointer to the std::string (that you have new:ed somewhere) you would naturallu have to call delete on it. If it is NOT a pointer (ie just a "normal" member) I think your leak origins elsewhere



/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
Thanks for the info, the static destroyInstance method was so obvious I didn't think of it :-/

I will look further into the string issue, as the memory leaks data contains the data that that particular string holds.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top