Sign in to follow this  

How do I get rid of memory leaks?

This topic is 4338 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well, I've started using Paul Nettle's memory manager (which is excellent, by the way), which has identified plenty of leaks. The problem is, I'm not really too sure about how to fix them, being a bit rubbish at C++ :) Take, for example, this bit of code:
SomeClass::AddThingsToList() {
	list.push_back(new OtherClass());
	list.push_back(new OtherClass());
	list.push_back(new OtherClass());
}
How on earth do I go about freeing up the memory from the new OtherClasses? Do I do it on program exit by using list.erase()? That's how I thought it would work, but it's still reporting that the allocations made on these lines aren't being deallocated. What about deleting things like singletons? I'm using the singleton class from Richard Fine's Enginuity series, which works wonderfully, except that calling Whatever::Destroy() or Whatever::GetSingleton().Destroy() still reports that the memory hasn't been deallocated. The Destroy method deletes the reference to the singleton and sets it to 0. What am I doing wrong? Thanks in advance for your help, folks, you're a jolly helpful bunch :)

Share this post


Link to post
Share on other sites
*Hugs smart pointers and runs away to hide somewhere*

Well I'd assume(now this is a tired post and I'm bound to be wrong) that you just do delete list[i]; or something of the sort.

Actualy come to think of it I'm almost posotive that's 100% wrong.

Share this post


Link to post
Share on other sites
As far as the standard containers go, they don't know that they're storing pointers per se, and so won't delete that memory for you. Rather than using erase(), you'll need to delete them manually, like 'delete *iter' or something similar. You could also use a smart pointer class that will handle the cleanup for you.

Not sure about the singleton...

Share this post


Link to post
Share on other sites

This topic is 4338 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this