Linux下利用Valgrind工具进行内存泄露检测

Valgrind通常用来成分析程序性能及程序中的内存泄露错误

一 Valgrind工具集简绍

Valgrind包含下列工具:
1、memcheck:检查程序中的内存问题,如泄漏、越界、非法指针等。
2、callgrind:检测程序代码的运行时间和调用过程,以及分析程序性能。
3、cachegrind:分析CPU的cache命中率、丢失率,用于进行代码优化。
4、helgrind:用于检查多线程程序的竞态条件。
5、massif:堆栈分析器,指示程序中使用了多少堆内存等信息。
6、lackey:
7、nulgrind:
这几个工具的使用是通过命令:valgrand --tool=name 程序名来分别调用的,当不指定tool参数时默认是 --tool=memcheck

二 Valgrind工具详解

1.Memcheck
最常用的工具,用来检测程序中出现的内存问题,所有对内存的读写都会被检测到,一切对malloc、free、new、delete的调用都会被捕获。所以,它能检测以下问题:
1、对未初始化内存的使用;
2、读/写释放后的内存块;
3、读/写超出malloc分配的内存块;
4、读/写不适当的栈中内存块;
5、内存泄漏,指向一块内存的指针永远丢失;
6、不正确的malloc/free或new/delete匹配;
7、memcpy()相关函数中的dst和src指针重叠。
这些问题往往是C/C++程序员最头疼的问题,Memcheck能在这里帮上大忙。
例如:

#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  

案例介绍:
申请内存未释放:

#include <iostream>
using namespace std;

int main()
{
    int *a = new int(2);
    //delete a;
    return 0;
}
xchonxu/testCode> g++ -g -o testValgrind testValgrind.cc
xchonxu/testCode> ~/bin/bin/valgrind --tool=memcheck --leak-check=full ./testValgrind
==3598== Memcheck, a memory error detector
==3598== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==3598== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==3598== Command: ./testValgrind
==3598== 
==3598== 
==3598== HEAP SUMMARY:
==3598==     in use at exit: 4 bytes in 1 blocks
==3598==   total heap usage: 1 allocs, 0 frees, 4 bytes allocated
==3598== 
==3598== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==3598==    at 0x4C292DF: operator new(unsigned long) (vg_replace_malloc.c:332)
==3598==    by 0x4007AF: main (testValgrind.cc:7)
==3598== 
==3598== LEAK SUMMARY:
==3598==    definitely lost: 4 bytes in 1 blocks
==3598==    indirectly lost: 0 bytes in 0 blocks
==3598==      possibly lost: 0 bytes in 0 blocks
==3598==    still reachable: 0 bytes in 0 blocks
==3598==         suppressed: 0 bytes in 0 blocks
==3598== 
==3598== For counts of detected and suppressed errors, rerun with: -v
==3598== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)

Memcheck将内存泄露分为两种,一种是可能的内存泄露(Possibly lost),另外一种是确定的内存泄露(Definitely lost)。Possibly lost 是指仍然存在某个指针能够访问某块内存,但该指针指向的已经不是该内存首地址。Definitely lost 是指已经不能够访问这块内存。而Definitely lost又分为两种:直接的(direct)和间接的(indirect)。直接和间接的区别就是,直接是没有任何指针指向该内存,间接是指指向该内存的指针都位于内存泄露处。在上述的例子中,根节点是directly lost,而其他节点是indirectly lost
.
常用命令:
valgrind --log-file=./valgrind_report.log --tool=memcheck --leak-check=full --show-leak-kinds=all --show-reachable=yes --track-origins=yes -v ./(执行目标)
其他博主的详细介绍
有示例:https://www.cnblogs.com/dongdongwq/p/5377343.html
转载:https://www.linuxidc.com/Linux/2012-06/63754.htm
转载:https://www.cnblogs.com/AndyStudy/p/6409287.html(有安装原理介绍)

猜你喜欢

转载自blog.csdn.net/qq_34595352/article/details/88577784