1、安装mtrace工具
centos : sudo yum install glibc-utils
2、mtrace工具使用
/*************************************************************************
> File Name: mtrace.c
> Author: jinshaohui
> Mail: [email protected]
> Time: 18-11-07
> Desc:
************************************************************************/
#include<assert.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<mcheck.h>
int main()
{
int *p = NULL;
setenv("MALLOC_TRACE","1.log",1);
mtrace();
p = malloc(100);
muntrace();
return;
}
编译的时候需要加-g
[jsh@localhost work]$ gcc -g mtrace.c
[jsh@localhost work]$
[jsh@localhost work]$ ./a.out
[jsh@localhost work]$ mtrace a.out 1.log
Memory not freed:
-----------------
Address Size Caller
0x0904c428 0x64 at /home/jsh/workspace/Linux-C-C--learning/C/work/mtrace.c:21
很容易看出23行存在内存泄漏。