Thursday, May 29, 2008

How to detect Memory leak

1, The simple way must be use mtrace() at the first line of the main() function, and add muntrace() at the end line of main() function. You should include <mcheck.h> in the source code. Then you can do it like:
setenv MALLOC_TRACE /home/liu/tmp/trace.txt
g++ -g -o yourPro yourPro.cpp
./yourPro.cpp
mtrace youPro /home/liu/tmp/trace.txt
Then you will see the report in trace.txt..
Memory not freed:
-----------------
Address Size Caller
0x0804f0c0 0x35 at 0xb7c88327
0x0804f100 0x11 at 0xb7c88327
0x0804fd08 0x28 at 0xb7c88327
0x0804ff50 0x40 at 0xb7edcdba
0x08050000 0x40 at 0xb7edcdba
0x080500c0 0x94 at 0xb7edcdba
0xb6f9b008 0xc6268 at 0xb7edcdba
0xb7062008 0xc6268 at 0xb7edcdba
0xb71ad008 0x5d230 at 0xb7edcdba

As you see, I get some ASCII codes. It's not readable...and not like some other guys said it will be output by character.

Of course ,you can find its meaning by looking for the ascii table. The table comes from http://www.cplusplus.com/doc/ascii.html

2,using valgrind..
It's a tool for detecting the memory leak and other errors.. If you use the suse linux, you need not to download and install it.. Because it's a library in linux system.
If you want to know more you can "man valgrind" or to see "http://www.valgrind.org/"..The simple way to use it is :

valgrind --leak-check=yes ./YourApp

Then you will get a lot of informations about memory using.


In addition, there are many method to detect and trace the memory.
You can see "http://users.actcom.co.il/~choo/lupg/tutorials/unix-memory/unix-memory.html#tools_leaks"

No comments: