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.
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.
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).
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.