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

144 篇文章 13 订阅

https://blog.csdn.net/stpeace/article/details/61622214

https://www.cnblogs.com/AndyStudy/p/6409287.html

https://blog.csdn.net/primeprime/article/details/79539504

在前面的文章中, 我们简单了解了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/happylzs2008/article/details/94374511?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值