#include <stdlib.h> #include <mcheck.h> void func(void) { int* ptr = malloc(10 * sizeof(int)); free(ptr); } int main(void) { setenv("MALLOC_TRACE", "output_file_name", 1); mtrace(); func(); return 0; } # more output_file_name <-執行方法 = Start @ ./test:[0x8048426] + 0x804a470 0x28 @ ./test:[0x8048434] - 0x804a470 @ /lib/tls/i686/cmov/libc.so.6:(clearenv+0x7c)[0xb7e149ac] - 0x804a008 @ /lib/tls/i686/cmov/libc.so.6:(tdestroy+0x47)[0xb7ebc6d7] - 0x804a0c0 @ /lib/tls/i686/cmov/libc.so.6:(tdestroy+0x4f)[0xb7ebc6df] - 0x804a0e8 0x8048426 分配内存空间的指令地址 0x804a470 malloc分配的内存空间己地址 + 表示分配内存 - 表示释放内存 # mtrace output_file_name - 0x0804a008 Free 4 was never alloc'd 0xb7e149ac - 0x0804a0c0 Free 5 was never alloc'd 0xb7ebc6d7 - 0x0804a0e8 Free 6 was never alloc'd 0xb7ebc6df No memory leaks.
ref:若沒有 free 的話 Memory not freed: ----------------- Address Size Caller 0x0804a470 0x28 at 0x80483e6
weizhiyuan420的空间
二. valgrindInstall:
$ sudo apt-get install valgrind
$ sudo apt-get install valgrind
or
$ git clone git://sourceware.org/git/valgrind.git
$ cd valgrind; ./autogen.sh amd64-linux (or arm-linux); ./configure --prefix=/usr/local/valgrind;
$ make; make install
如果不make intall的話
如果不make intall的話
$ export VALGRIND_LIB=/home/ubuntu/valgrind/.in_place
Usage:
$ valgrind --leak-check=full --show-leak-kinds=all --verbose ./execute_name
or
$ valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose --log-file=output.txt ./executable parameter
沒有留言:
張貼留言