How to use dmalloc
1. check whether dmalloc has been installed in your computer
Normally dmalloc has been installed in the system, you can check this using: gyliu@egodev05-35: whereis dmalloc
dmalloc: /usr/bin/dmalloc /usr/include/dmalloc.h
If it is not installed in your computer, you can get src from /scratch/dev/ego/gyliu/dmalloc-5.5.2, compile and install it on your computer, the process is same as building 3rd party library.
2. setenv
Tcsh: setenv DMALLOC_OPTIONS "log=logname,debug=0x3"
Bash: export DMALLOC_OPTIONS=log=logname,debug=0x3
Note: logname is the file that will record your compile info.
3. You need to include dmalloc.h in your source code.
#include <dmalloc.h>
4. A simple example:
1 /* test.c for showing you how dmalloc works*/
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <dmalloc.h>
5
6 void swap(int *a, int *b)
7 {
8 *a = *a ^ *b;
9 *b = *a ^ *b;
10 *a = *a ^ *b;
11 }
12
13 int main()
14 {
15 char *str = malloc(100); /* malloc but not freed */
16 int a = 9;
17 int b = 8;
18 swap(&a, &b);
19 printf("%d ", a);
20 printf("%d/n ", b);
21 }
setenv DMALLOC_OPTIONS "log=xjtuse,debug=0x3"
gcc -g t.c –ldmalloc
Then in your directory will generate your binary a.out and compiling record file xjtuse. Open xjutse, you can see it like this:
gyliu@egodev05-60: cat xjtuse
1193233343: 1: Dmalloc version '5.3.0' from 'http://dmalloc.com/'
1193233343: 1: flags = 0x3, logfile 'xjtuse'
1193233343: 1: interval = 0, addr = 0, seen # = 0, limit = 0
1193233343: 1: starting time = 1193233343
1193233343: 1: process pid = 24116
1193233343: 1: Dumping Chunk Statistics:
1193233343: 1: basic-block 4096 bytes, alignment 8 bytes, heap grows up
1193233343: 1: heap address range: 0x9a73000 to 0x9a79000, 24576 bytes
1193233343: 1: user blocks: 1 blocks, 4068 bytes (16%)
1193233343: 1: admin blocks: 5 blocks, 20480 bytes (83%)
1193233343: 1: external blocks: 0 blocks, 0 bytes (0%)
1193233343: 1: total blocks: 6 blocks, 24576 bytes
1193233343: 1: heap checked 0
1193233343: 1: alloc calls: malloc 1, calloc 0, realloc 0, free 0
1193233343: 1: alloc calls: recalloc 0, memalign 0, valloc 0
1193233343: 1: alloc calls: new 0, delete 0
1193233343: 1: current memory in use: 100 bytes (1 pnts)
1193233343: 1: total memory allocated: 100 bytes (1 pnts)
1193233343: 1: max in use at one time: 100 bytes (1 pnts)
1193233343: 1: max alloced with 1 call: 100 bytes
1193233343: 1: max unused memory space: 28 bytes (21%)
1193233343: 1: top 10 allocations:
1193233343: 1: total-size count in-use-size count source
1193233343: 1: 100 1 100 1 t.c:15
1193233343: 1: 100 1 100 1 Total of 1
1193233343: 1: Dumping Not-Freed Pointers Changed Since Start:
1193233343: 1: not freed: '0x9a73f80|s1' (100 bytes) from 't.c:15'
1193233343: 1: total-size count source
1193233343: 1: 100 1 t.c:15
1193233343: 1: 100 1 Total of 1
1193233343: 1: ending time = 1193233343, elapsed since start = 0:00:00
gyliu@egodev05-61: cat xjtuse | grep freed
1193233343: 1: not freed: '0x9a73f80|s1' (100 bytes) from 't.c:15'
This way, you can check which line you have malloc/calloc memory but did not free the memory.