简介:
valgrind检测内存泄露工具。功能非常强大,具体详见http://www.valgrind.org/。
这里只介绍内存检测部分。
参考实例:
http://www.ibm.com/developerworks/cn/linux/l-cn-valgrind/
安装:
① 下载
http://www.valgrind.org/
② 安装
tar jxvf valgrind-3.8.1.tar.bz2
cd valgrind-3.8.1
./configure --prefix=/usr
make
make install
使用:
valgrind --tool=memcheck --leak-check=full ./test
参照:
http://www.valgrind.org/docs/manual/quick-start.html
实例:
① 手顺
gcc test.c -o test
valgrind --tool=memcheck --leak-check=full ./test
② 代码
1 //gcc test.c -o test 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 int main(int argc, char **argv) 6 { 7 char *string; 8 string = (char*)malloc(sizeof(char)); 9 string = (char*)malloc(sizeof(int*)); 10 return 0; 11 }
③ 解析结果
==13226== Memcheck, a memory error detector
==13226== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==13226== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==13226== Command: ./test
==13226==
==13226==
==13226== HEAP SUMMARY:
==13226== in use at exit: 5 bytes in 2 blocks
==13226== total heap usage: 2 allocs, 0 frees, 5 bytes allocated
==13226==
==13226== 1 bytes in 1 blocks are definitely lost in loss record 1 of 2
==13226== at 0x4029D56: malloc (vg_replace_malloc.c:270)
==13226== by 0x80483D8: main (test.c:8)
==13226==
==13226== 4 bytes in 1 blocks are definitely lost in loss record 2 of 2
==13226== at 0x4029D56: malloc (vg_replace_malloc.c:270)
==13226== by 0x80483E8: main (test.c:9)
==13226==
==13226== LEAK SUMMARY:
==13226== definitely lost: 5 bytes in 2 blocks
==13226== indirectly lost: 0 bytes in 0 blocks
==13226== possibly lost: 0 bytes in 0 blocks
==13226== still reachable: 0 bytes in 0 blocks
==13226== suppressed: 0 bytes in 0 blocks
==13226==
==13226== For counts of detected and suppressed errors, rerun with: -v
==13226== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 11 from 6)