How to use dmalloc

本文介绍如何利用dmalloc工具检查程序中的内存泄漏问题。包括安装配置dmalloc、设置环境变量记录编译信息、在源代码中引入dmalloc.h文件等步骤,并通过一个示例程序演示如何发现未释放的内存分配。
 
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.
 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值