在前面的文章中, 我们简单了解了valgrind工具的用途以及安装, 以便大家能进行实际操作。 在本文中, 我们通过实例来看看如何利用valgrind来定位内存泄漏问题。 先看程序:
#include <stdio.h>#include <stdlib.h>char* getMemory(){ char *p = (char *)malloc(30); return p;}int main(){ char *p = getMemory(); p = NULL; return 0;}
只要是懂一点C/C++的人, 就很容易看出上述程序有内存泄漏, 我们用valgrind工具来检测下:
[root@xxx ~/valgrind-3.8.1/bin]# ./valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./a.out==19226== Memcheck, a memory error detector==19226== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.==19226== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info==19226== Command: ./a.out==19226== ==19226== ==19226== HEAP SUMMARY:==19226== in use at exit: 30 bytes in 1 blocks==19226== total heap usage: 1 allocs, 0 frees, 30 bytes allocated==19226== ==19226== 30 bytes in 1 blocks are definitely lost in loss record 1 of 1==19226== at 0x4C278FE: malloc (vg_replace_malloc.c:270)==19226== by 0x4005B5: getMemory() (in /root/valgrind-3.8.1/bin/a.out)==19226== by 0x4005CC: main (in /root/valgrind-3.8.1/bin/a.out)==19226== ==19226== LEAK SUMMARY:==19226== definitely lost: 30 bytes in 1 blocks==19226== indirectly lost: 0 bytes in 0 blocks==19226== possibly lost: 0 bytes in 0 blocks==19226== still reachable: 0 bytes in 0 blocks==19226== suppressed: 0 bytes in 0 blocks==19226== ==19226== For counts of detected and suppressed errors, rerun with: -v==19226== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)[root@xxx ~/valgrind-3.8.1/bin]#
我们可以很清楚地看到, 在getMemory调用malloc的那里, 有内存泄漏。最左边的19226表示进程号!
不过, 这样看着也挺蛋疼的, 如果代码过多, 肉眼据不太好分析了, 能不能把内存泄漏的代码行给找出来呢? 当然能! 回想一下我们之前介绍过得core dump定位到代码行的问题, 两个必要条件是: 编译时必须有-g参数; 编译后不能strip. 我们一起再看看:
[root@xxx ~/valgrind-3.8.1/bin]# g++ -g test.cpp [root@xxx ~/valgrind-3.8.1/bin]# [root@xxx ~/valgrind-3.8.1/bin]# ./valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./a.out==20448== Memcheck, a memory error detector==20448== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.==20448== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info==20448== Command: ./a.out==20448== ==20448== ==20448== HEAP SUMMARY:==20448== in use at exit: 30 bytes in 1 blocks==20448== total heap usage: 1 allocs, 0 frees, 30 bytes allocated==20448== ==20448== 30 bytes in 1 blocks are definitely lost in loss record 1 of 1==20448== at 0x4C278FE: malloc (vg_replace_malloc.c:270)==20448== by 0x4005B5: getMemory() (test.cpp:5)==20448== by 0x4005CC: main (test.cpp:11)==20448== ==20448== LEAK SUMMARY:==20448== definitely lost: 30 bytes in 1 blocks==20448== indirectly lost: 0 bytes in 0 blocks==20448== possibly lost: 0 bytes in 0 blocks==20448== still reachable: 0 bytes in 0 blocks==20448== suppressed: 0 bytes in 0 blocks==20448== ==20448== For counts of detected and suppressed errors, rerun with: -v==20448== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)[root@xxx ~/valgrind-3.8.1/bin]#
好了, 泄漏的代码行出来了。 这里, 我顺便说说以往介绍过得addr2line命令, 如果用addr2line -e a.out 0x4005B5, 也是能得出代码行的。
fix后的代码如下:
#include <stdio.h>#include <stdlib.h>char* getMemory(){ char *p = (char *)malloc(30); return p;}int main(){ char *p = getMemory(); if(p != NULL) { free(p); p = NULL; } return 0;}
我们再用valgrind工具检测一下:
[root@xxx ~/valgrind-3.8.1/bin]# g++ -g test.cpp [root@xxx ~/valgrind-3.8.1/bin]# [root@xxx ~/valgrind-3.8.1/bin]# ./valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./a.out==21033== Memcheck, a memory error detector==21033== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.==21033== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info==21033== Command: ./a.out==21033== ==21033== ==21033== HEAP SUMMARY:==21033== in use at exit: 0 bytes in 0 blocks==21033== total heap usage: 1 allocs, 1 frees, 30 bytes allocated==21033== ==21033== All heap blocks were freed -- no leaks are possible==21033== ==21033== For counts of detected and suppressed errors, rerun with: -v==21033== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)[root@xxx ~/valgrind-3.8.1/bin]#
可见, 没有内存泄漏了。 爽爽哒
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow