简介:
memwatch简单易用,能检测未被释放的内存,释放多次的内存。
安装:
① 下载
http://www.linkdata.se/sourcecode/memwatch/
② 解压
找出 memwatch.c、memwatch.h即可。
使用:
① 编译时,需追加MEMWATCH(使用memwacth,详见memwatch.h)、MW_STDIO(在控制台输出提示)宏值
实例:
① 手顺
gcc -DMEMWATCH -DMW_STDIO test.c memwatch.c -o test
./test
② 代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <errno.h> 4 #ifdef MEMWATCH 5 #include "memwatch.h" 6 #endif 7 8 void hello() 9 { 10 char *hello; 11 if ((hello = (char *) malloc(sizeof(char))) == NULL) { 12 perror("Cannot allocate memory."); 13 return; 14 } 15 } 16 17 int main() 18 { 19 hello(); 20 return 0; 21 }
③ 解析结果
============= MEMWATCH 2.71 Copyright (C) 1992-1999 Johan Lindh =============
Started at Wed Sep 4 16:21:26 2013
Modes: __STDC__ 64-bit mwDWORD==(unsigned long)
mwROUNDALLOC==8 sizeof(mwData)==32 mwDataSize==32
Stopped at Wed Sep 4 16:21:26 2013
unfreed: <1> test.c(12), 1 bytes at 0x9dcb1f0 {FE .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .}
Memory usage statistics (global):
N)umber of allocations made: 1
L)argest memory usage : 1
T)otal of all alloc() calls: 1
U)nfreed bytes totals : 1