Click here to Skip to main content
15,867,453 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?
Posted

Did you try with the debugger ? After running your debugger, you always have a trace of memory allocation that were not deleted.
 
Share this answer
 
Comments
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
#endif
 
Share this answer
 
If you can handle the truth, ask your mentor for a code review.
 
Share this answer
 
Better when you play with memory , try to take care of it...
If allocate then deallocate , check bounds etc

http://simplestcodings.blogspot.com/[^]
 
Share this answer
 
Here you are: Visual Leak Detector[^]
 
Share this answer
 
bound checker is a helpful tool
 
Share this answer
 
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.

Cheers,

Ash
 
Share this answer
 
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.
Tips:
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:
C++
void foo()
{
 int* arr = new int[500];
}

void bar()
{
 int* arr = new int;
}

void caller()
{
 foo();
 for(int i = 0; i < 10000; ++i)
  bar();
}

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. http://msdn.microsoft.com/en-us/library/x98tx3cf%28v=vs.80%29.aspx[^] . This is an article for windows visual studio users.
4. http://www.linuxjournal.com/article/6556[^] . Nice introduction.
5. http://www.linkdata.se/sourcecode/memwatch/[^]
6. Memory Leak Detection[^]
7. http://wyw.dcweb.cn/leakage.htm[^] . Nice article, if you go through it carefully, you can roll you own memory debugger also. ;)
 
Share this answer
 
v2

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