how to find a memory leak?

Apr 22, 2011 at 11:27am
hi all!
I am creating a quite big project and I noticed that in some particular situations there are small memory leaks...
I am calling many malloc, free etc...
Is there an effective way to find out where the memory leak is coming from?
perhaps with a debug??
i'm programming in linux with code blocks and gcc.

thanks
Marco
Apr 22, 2011 at 11:33am
Yes, there is valgrind for that.
Apr 22, 2011 at 11:51am
The rough universal-valid method is to comment parts of your code starting from the initial object declarations and first function calls, and then continue narrowing that memory-leaking code commenting. In the end you'll get a small-enough piece of code where it should be easy enough to spot the problem.
Apr 22, 2011 at 12:25pm
Do you know what the particular situations are?
Apr 22, 2011 at 1:59pm
I am calling many malloc, free etc...


If your using C++ then you shouldn't be using malloc.

Also again if your porgramming in C++ then it might be wise to make the destructers for your classess virtual(only applies if your also using inheritence).
Apr 22, 2011 at 2:13pm
You need to audit your code. Look through, finding every single call to malloc and make absolutely sure there is a call to free for every one. You also need to check that your pointers don't get "re-pointed" before you call free.

I remember a project I wrote (it was sort of like an archiving program, so it copied a group of files into a single file with a header so you could find out the start and end of each file) which had lots of memory leaks where I had allocated memory in a loop by accident and other times where I had re-used a point. I used Valgrind to tell me how many calls to malloc() there were (about 1,900) and how many calls to free() there were (less than 1,000) and just kept editing my code until there was the same amout of calls to malloc() and free() (it settled at around 1,200).
Apr 22, 2011 at 3:20pm
I think king214 is right, it's about time you upgraded your C code to C++ code and used new and delete. And then for every new there must be a delete; simple, eh?

Apr 22, 2011 at 3:27pm
new yes - however, delete is rarely used in C++ code when conforming to RAII.
Memory leaks are therefore almost a non-issue in C++.
Apr 22, 2011 at 3:41pm
Forgive me, I'm still new to C++ (pun not intended):

I thought that new doesn't conform with RAII and that if new is used it doesn't matter if you leave the block; the resource is still there, so delete is necessary.
Apr 22, 2011 at 3:46pm
That is correct. RAII essentially states that resources must be bound to the lifetime of objects, so if you create an object using new, you'll have to insert it into an appropriate container immediately (such as a smart pointer). When the scope is left, the container will be destroyed and it is the container's job to delete the object when appropriate.
So your own code should rarely contain any deletes, except when implementing some sort of container.
Last edited on Apr 22, 2011 at 3:48pm
Apr 22, 2011 at 3:50pm
So therefore, you do have to use delete, just wanted to make sure. I nearly lost all faith in what I knew about dynamic programming...
Apr 22, 2011 at 4:17pm
Well, the point is that you usually don't use delete yourself, only the library writers do (stdlib, boost, etc.).
Apr 22, 2011 at 5:29pm
I guess you really do learn something new everyday, never knew it. Thanks!
Topic archived. No new replies allowed.