linux下利用valgrind工具进行内存泄露检测

 

先查找:yum search valgrind
再安装:yum -y install valgrind.x86_64 


2.1 valgrind介绍
开源的valgrind工具还是相当不错的

  • Memcheck。这是valgrind应用最广泛的工具,一个重量级的内存检查器,能够发现开发中绝大多数内存错误使用情况,比如:使用未初始化的内存,使用已经释放了的内存,内存访问越界等。
  • Callgrind。它主要用来检查程序中函数调用过程中出现的问题。
  • Cachegrind。它主要用来检查程序中缓存使用出现的问题。
  • Helgrind。它主要用来检查多线程程序中出现的竞争问题。
  • Massif。它主要用来检查程序中堆栈使用中出现的问题。
  • Extension。可以利用core提供的功能,自己编写特定的内存调试工具

Memcheck

最常用的工具,用来检测程序中出现的内存问题,所有对内存的读写都会被检测到,一切对malloc()/free()/new/delete的调用都会被捕获。

内容    描述
使用未初始化的内存    Use of uninitialised memory
使用已经释放了的内存    Reading/writing memory after it has been free’d
使用超过 malloc分配的内存空间    Reading/writing off the end of malloc’d blocks
对堆栈的非法访问    Reading/writing inappropriate areas on the stack
申请的空间是否有释放    Memory leaks – where pointers to malloc’d blocks are lost forever
malloc/free/new/delete申请和释放内存的匹配    Mismatched use of malloc/new/new [] vs free/delete/delete []
src和dst的重叠    Overlapping src and dst pointers in memcpy() and related functions

2.4 使用说明
编译程序

g++ -g -o leak leak.c 
被检测程序加入 –g保留调试信息, 否则后面的valgrind不能显示到出错行号。

valgrind被设计成非侵入式的,它直接工作于可执行文件上,因此在检查前不需要重新编译、连接和修改你的程序。(如果不知道有哪些参数, 可以先输入valgrind –tool=, 然后狂按两次tab)

2.5 使用valgrind检测Memcheck
下面我们就可以用valgrind对我们的程序检测leak

valgrind --tool=memcheck --leak-check=full --show-reachable=yes --trace-children=yes   ./leak
  1. –leak-check=full 指的是完全检查内存泄漏,
  2. –show-reachable=yes是显示内存泄漏的地点,
  3. –trace-children=yes是跟入子进程。

当程序正常退出的时候,输出:

è¾åºåå­æ³æ¼çä¿¡æ¯å

另外一个例子:

#include <stdlib.h>
#include <malloc.h>
#include <string.h>

void test()
{
    int *ptr = malloc(sizeof(int)*10);

    ptr[10] = 7; // 内存越界

    memcpy(ptr +1, ptr, 5); // 踩内存


    free(ptr); 
    free(ptr);// 重复释放

    int *p1;
    *p1 = 1; // 非法指针
}

int main(void)
{
    test();
    return 0;
}

执行:valgrind --leak-check=full ./程序名
输出结果如下:

==4832== Memcheck, a memory error detector
==4832== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==4832== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==4832== Command: ./tmp
==4832== 
==4832== Invalid write of size 4      // 内存越界
==4832==    at 0x804843F: test (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==  Address 0x41a6050 is 0 bytes after a block of size 40 alloc'd
==4832==    at 0x4026864: malloc (vg_replace_malloc.c:236)
==4832==    by 0x8048435: test (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832== 
==4832== Source and destination overlap in memcpy(0x41a602c, 0x41a6028, 5) // 踩内存
==4832==    at 0x4027BD6: memcpy (mc_replace_strmem.c:635)
==4832==    by 0x8048461: test (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832== 
==4832== Invalid free() / delete / delete[] // 重复释放
==4832==    at 0x4025BF0: free (vg_replace_malloc.c:366)
==4832==    by 0x8048477: test (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==  Address 0x41a6028 is 0 bytes inside a block of size 40 free'd
==4832==    at 0x4025BF0: free (vg_replace_malloc.c:366)
==4832==    by 0x804846C: test (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832== 
==4832== Use of uninitialised value of size 4 // 非法指针
==4832==    at 0x804847B: test (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832== 
==4832== 
==4832== Process terminating with default action of signal 11 (SIGSEGV) //由于非法指针赋值导致的程序崩溃
==4832==  Bad permissions for mapped region at address 0x419FFF4
==4832==    at 0x804847B: test (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832== 
==4832== HEAP SUMMARY:
==4832==     in use at exit: 0 bytes in 0 blocks
==4832==   total heap usage: 1 allocs, 2 frees, 40 bytes allocated
==4832== 
==4832== All heap blocks were freed -- no leaks are possible
==4832== 
==4832== For counts of detected and suppressed errors, rerun with: -v
==4832== Use --track-origins=yes to see where uninitialised values come from
==4832== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 11 from 6)
Segmentation fault

从valgrind的检测输出结果看,这几个错误都找了出来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值