Skip to content

Commit 02ec760

Browse files
author
Wang Jiazi
committed
The mtrace() function installs hook functions for the memory-allocation functions (malloc(3), realloc(3) memalign(3), free(3)). These hook functions record tracing information about memory
allocation and deallocation. The tracing information can be used to discover memory leaks and attempts to free nonallocated memory in a program.
1 parent bced909 commit 02ec760

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Linux/Mtrace/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mtrace_test: mtrace_test.cpp
2+
g++ $^ -o $@ -g3 -std=c++11
3+
4+
dump: mtrace_test
5+
mtrace $^ output

Linux/Mtrace/mtrace_test.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
#include <mcheck.h>
3+
#include <stdlib.h>
4+
using namespace std;
5+
6+
struct MemBlock
7+
{
8+
char data[100];
9+
};
10+
int main()
11+
{
12+
setenv("MALLOC_TRACE","output",1);
13+
mtrace();
14+
int *p1=new int;
15+
16+
MemBlock *p2=new MemBlock;
17+
int *p3=(int*)malloc(sizeof(int));
18+
int *p4=(int*)malloc(sizeof(int));
19+
int *p5=(int*)malloc(sizeof(int)*16);
20+
21+
delete p1;
22+
free(p3);
23+
return 0;
24+
}

0 commit comments

Comments
 (0)