使用过程:
- 程序中需要包含头文件mchech.h, 在程序开始处调用mtrace()
- 设定环境变量 export MALLOC_TRACE=”mtrace.out”
- 编译运行程序, 会生成mtrace.out文件
- mtrace a.out mtrace.out得到内存泄露信息
Memory not freed:
Address Size Caller
0x0000000001650490 0x28 at /vm//src/mtrace_test/mtrace_test.c:11
0x00000000016504f0 0x28 at /vm//src/mtrace_test/mtrace_test.c:13
0x0000000001650550 0xa at /vm/***/src/mtrace_test/mtrace_test.c:15
1 | /* mtrace_test.c */ |
参考:
[1] http://www.gnu.org/software/libc/manual/html_node/Allocation-Debugging.html
[2] http://www.gnu.org/software/libc/manual/html_node/Hooks-for-Malloc.html