0%

mtrace使用笔记

使用过程:

  1. 程序中需要包含头文件mchech.h, 在程序开始处调用mtrace()
  2. 设定环境变量 export MALLOC_TRACE=”mtrace.out”
  3. 编译运行程序, 会生成mtrace.out文件
  4. 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* mtrace_test.c */
#include <stdio.h>
#include <stdlib.h>
#include <mcheck.h>
#
int main(void)
{
mtrace(); 

int i;
int *p_0 = (int*)malloc(sizeof(int)*10);
int *p_1 = (int*)malloc(sizeof(int)*10);
int *p_2 = (int*)malloc(sizeof(int)*10);
int *p_3 = (int*)malloc(sizeof(int)*10);
int *p_4 = (int*)malloc(sizeof(int)*10);
char *p_char = (char*)malloc(sizeof(char)*10);

for (i = 0; i < 10; i++) {
p_0[i] = i;
}

for (i = 0; i < 10; i++) {
p_char[i] = 'w';
}

free(p_0);
/* 制造人为的内存泄漏 */
//free(p_1)
free(p_2);
//free(p_3)
free(p_4);
//free(p_char);

return 0;
}

参考:

[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