实例介绍利用valgrind定位内存泄漏问题

来源:互联网 发布:河南民生频道网络直播 编辑:程序博客网 时间:2024/06/06 04:48

 在前面的文章中, 我们简单了解了valgrind工具的用途以及安装, 以便大家能进行实际操作。 在本文中, 我们通过实例来看看如何利用valgrind来定位内存泄漏问题。 先看程序:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. char* getMemory()  
  4. {  
  5.     char *p = (char *)malloc(30);   
  6.     return p;  
  7. }  
  8.   
  9. int main()  
  10. {  
  11.     char *p = getMemory();  
  12.     p = NULL;  
  13.       
  14.     return 0;  
  15. }  

       只要是懂一点C/C++的人, 就很容易看出上述程序有内存泄漏, 我们用valgrind工具来检测下:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@xxx ~/valgrind-3.8.1/bin]# ./valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./a.out  
  2. ==19226== Memcheck, a memory error detector  
  3. ==19226== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.  
  4. ==19226== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info  
  5. ==19226== Command: ./a.out  
  6. ==19226==   
  7. ==19226==   
  8. ==19226== HEAP SUMMARY:  
  9. ==19226==     in use at exit: 30 bytes in 1 blocks  
  10. ==19226==   total heap usage: 1 allocs, 0 frees, 30 bytes allocated  
  11. ==19226==   
  12. ==19226== 30 bytes in 1 blocks are definitely lost in loss record 1 of 1  
  13. ==19226==    at 0x4C278FE: malloc (vg_replace_malloc.c:270)  
  14. ==19226==    by 0x4005B5: getMemory() (in /root/valgrind-3.8.1/bin/a.out)  
  15. ==19226==    by 0x4005CC: main (in /root/valgrind-3.8.1/bin/a.out)  
  16. ==19226==   
  17. ==19226== LEAK SUMMARY:  
  18. ==19226==    definitely lost: 30 bytes in 1 blocks  
  19. ==19226==    indirectly lost: 0 bytes in 0 blocks  
  20. ==19226==      possibly lost: 0 bytes in 0 blocks  
  21. ==19226==    still reachable: 0 bytes in 0 blocks  
  22. ==19226==         suppressed: 0 bytes in 0 blocks  
  23. ==19226==   
  24. ==19226== For counts of detected and suppressed errors, rerun with: -v  
  25. ==19226== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)  
  26. [root@xxx ~/valgrind-3.8.1/bin]#   

       我们可以很清楚地看到, 在getMemory调用malloc的那里, 有内存泄漏。最左边的19226表示进程号!


       不过, 这样看着也挺蛋疼的, 如果代码过多, 肉眼据不太好分析了, 能不能把内存泄漏的代码行给找出来呢? 当然能! 回想一下我们之前介绍过得core dump定位到代码行的问题, 两个必要条件是:  编译时必须有-g参数; 编译后不能strip.  我们一起再看看:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@xxx ~/valgrind-3.8.1/bin]# g++ -g test.cpp   
  2. [root@xxx ~/valgrind-3.8.1/bin]#   
  3. [root@xxx ~/valgrind-3.8.1/bin]# ./valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./a.out  
  4. ==20448== Memcheck, a memory error detector  
  5. ==20448== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.  
  6. ==20448== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info  
  7. ==20448== Command: ./a.out  
  8. ==20448==   
  9. ==20448==   
  10. ==20448== HEAP SUMMARY:  
  11. ==20448==     in use at exit: 30 bytes in 1 blocks  
  12. ==20448==   total heap usage: 1 allocs, 0 frees, 30 bytes allocated  
  13. ==20448==   
  14. ==20448== 30 bytes in 1 blocks are definitely lost in loss record 1 of 1  
  15. ==20448==    at 0x4C278FE: malloc (vg_replace_malloc.c:270)  
  16. ==20448==    by 0x4005B5: getMemory() (test.cpp:5)  
  17. ==20448==    by 0x4005CC: main (test.cpp:11)  
  18. ==20448==   
  19. ==20448== LEAK SUMMARY:  
  20. ==20448==    definitely lost: 30 bytes in 1 blocks  
  21. ==20448==    indirectly lost: 0 bytes in 0 blocks  
  22. ==20448==      possibly lost: 0 bytes in 0 blocks  
  23. ==20448==    still reachable: 0 bytes in 0 blocks  
  24. ==20448==         suppressed: 0 bytes in 0 blocks  
  25. ==20448==   
  26. ==20448== For counts of detected and suppressed errors, rerun with: -v  
  27. ==20448== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)  
  28. [root@xxx ~/valgrind-3.8.1/bin]#   

        好了, 泄漏的代码行出来了。 这里, 我顺便说说以往介绍过得addr2line命令, 如果用addr2line -e a.out 0x4005B5,  也是能得出代码行的。


        fix后的代码如下:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. char* getMemory()  
  4. {  
  5.     char *p = (char *)malloc(30);   
  6.     return p;  
  7. }  
  8.   
  9. int main()  
  10. {  
  11.     char *p = getMemory();  
  12.     if(p != NULL)  
  13.     {  
  14.         free(p);  
  15.         p = NULL;  
  16.     }  
  17.       
  18.     return 0;  
  19. }  

        我们再用valgrind工具检测一下:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [root@xxx ~/valgrind-3.8.1/bin]# g++ -g test.cpp   
  2. [root@xxx ~/valgrind-3.8.1/bin]#   
  3. [root@xxx ~/valgrind-3.8.1/bin]# ./valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./a.out  
  4. ==21033== Memcheck, a memory error detector  
  5. ==21033== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.  
  6. ==21033== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info  
  7. ==21033== Command: ./a.out  
  8. ==21033==   
  9. ==21033==   
  10. ==21033== HEAP SUMMARY:  
  11. ==21033==     in use at exit: 0 bytes in 0 blocks  
  12. ==21033==   total heap usage: 1 allocs, 1 frees, 30 bytes allocated  
  13. ==21033==   
  14. ==21033== All heap blocks were freed -- no leaks are possible  
  15. ==21033==   
  16. ==21033== For counts of detected and suppressed errors, rerun with: -v  
  17. ==21033== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)  
  18. [root@xxx ~/valgrind-3.8.1/bin]#   

       可见, 没有内存泄漏了。
0 0