how to find a memory leak?

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.

Yes, there is valgrind for that.
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.
Do you know what the particular situations are?
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).
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).
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?

new yes - however, delete is rarely used in C++ code when conforming to RAII.
Memory leaks are therefore almost a non-issue in C++.
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.
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
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...
Well, the point is that you usually don't use delete yourself, only the library writers do (stdlib, boost, etc.).
I guess you really do learn something new everyday, never knew it. Thanks!
Topic archived. No new replies allowed.