Click here to Skip to main content
14,865,088 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How to find out memory leakage detection in C++? It should give the exact location in code where there is memory leakage and help to debug it. How to remove or avoid memory leakage?

Did you try with the debugger ? After running your debugger, you always have a trace of memory allocation that were not deleted.
Nish Nishant 13-Jul-10 23:08pm
Reason for my vote of 5
Worth 5!
JackDingler 18-Nov-11 10:56am
And be sure you have this code block in your CPP files, after your header includes.

#ifdef _DEBUG
#define new DEBUG_NEW
If you can handle the truth, ask your mentor for a code review.
Better when you play with memory , try to take care of it...
If allocate then deallocate , check bounds etc[^]
Here you are: Visual Leak Detector[^]
bound checker is a helpful tool
Adding a bit to Niklas' answer above...

The best way of finding out where memory leaks are is to not make any. Then you know that you haven't got any blocks unfreed.

To get to this state of Nirvana:

- Don't manually manage memory, i.e. don't use new and delete (malloc and free in C)

- If you have to manually manage memory then immediately assign objects you create to something like std::auto_ptr or std::unique_ptr. Then you know that whatever happens the object will be deleted.


Can you tell the compiler suit you are using?
Removing or avoiding memory leak is tricky. You can use the smart pointers for this. But please be aware, to use smart pointer you have to use C++ compiler and also the ownership issue.
1)Ownership issue is important. So read the smart pointer manual carefully and ask ask question you have out here. But try to use a standard/tested smart pointer. (Recently I saw some memory leak due to a custom smart pointer we were using). Be careful about auto_ptr also. As far as I remember it has gone 3 revisions. So be careful.
2)Don't ignore small leaks. for example:
void foo()
 int* arr = new int[500];

void bar()
 int* arr = new int;

void caller()
 for(int i = 0; i < 10000; ++i)

Here as you can see function bar() is called repeatedly, more than foo(). So although bar() leaks little on its own, it leaks a huge when called repeatedly.
3) Sometime tools show some so called leaks, but in reality they may not be leaks. What happens is, some tools use a conservative garbage collector algorithm to detect leaks. Which appear to be a leak at one point of time, may not remain a leak when the program ends.

Few articles and tools that you will find good are:
1. libumem is good for solaris. Recently its ported to linux and windows too.
2. Valgrid is also useful for linux environments.
3.[^] . This is an article for windows visual studio users.
4.[^] . Nice introduction.
6. Memory Leak Detection[^]
7.[^] . Nice article, if you go through it carefully, you can roll you own memory debugger also. ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900