|
The GNU C library (glibc 2 a.k.a linux libc6) 提供了一些能够检测内存分配/使用问题的功能,比如使用未初始化的内存,忘记释放内存等。
环境变量MALLOC_CHECK_在前面已经介绍过,这里要介绍的是GNU的扩展,mcheck, 一个简单易用但也比较有限的工具。 使用mcheck需要包含头文件<mcheck.h>,并且在执行前调用mtrace()。而且它也不能给出更多信息,只能用作内存问题检测的参考工具。如果想在程序中间中止检测,可以调用muntrace()。
用法 $ export MALLOC_TRACE=<outputfilename> $ <runprog> $ mtrace <prog> <outputfilename>
例子如下: #include <mcheck.h> #include <stdlib.h> #include <stdio.h> #include <string.h>
int main(int argc, char** argv) { mtrace(); char * buf = (char*) malloc(1024); // free(buf);
return 0; } $ g++ test.cpp -o test $ ./test $ export MALLOC_TRACE=trace.log $ mtrace test trace.log
Memory not freed: ----------------- Address Size Caller 0x090673c8 0x400 at 0x80483ea
reference:
|