linux 安装valgrind

valgrind介绍


Valgrind是一套Linux下,开放源代码(GPL V2)的仿真调试工具的集合。Valgrind由内核(core)以及基于内核的其他调试工具组成。内核类似于一个框架(framework),它模拟了一个CPU环境,并提供服务给其他工具;而其他工具则类似于插件 (plug-in),利用内核提供的服务完成各种特定的内存调试任务。
Valgrind包括如下一些工具:

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

valgrind安装

 

1.valgrind下载:
http://valgrind.org/downloads/valgrind-3.12.0.tar.bz2
2.valgrind安装:

tar -jxvf valgrind-3.12.0.tar.bz2
cd valgrind-3.12.0
./configure
make
sudo make install

3.查看valgrind是否安装成功

执行 valgrind --help查看是否有帮助参数

valgrind使用

1 demo内存泄漏程序

#include <stdio.h>
#include <stdlib.h>

#include <iostream>
using namespace std;
int main()
{
        int* p = new int(10);
        while(1)
        {

        }
        return 0;
}

2 执行命令编译 

g++ -g main.cpp -o main

编译加-g参数

3 执行valgrind命令启动程序,并检测程序状态

valgrind --tool=memcheck --leak-check=full --show-reachable=yes --trace-children=yes    ./main

命令执行结束显示

==75816== Memcheck, a memory error detector
==75816== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==75816== Using Valgrind-3.17.0 and LibVEX; rerun with -h for copyright info
==75816== Command: ./main
==75816== 
^C==75816== 
==75816== Process terminating with default action of signal 2 (SIGINT)
==75816==    at 0x400749: main (main.cpp:13)
==75816== 
==75816== HEAP SUMMARY:
==75816==     in use at exit: 72,708 bytes in 2 blocks
==75816==   total heap usage: 2 allocs, 0 frees, 72,708 bytes allocated
==75816== 
==75816== 4 bytes in 1 blocks are still reachable in loss record 1 of 2
==75816==    at 0x4C297A8: operator new(unsigned long) (vg_replace_malloc.c:417)
==75816==    by 0x40073E: main (main.cpp:8)
==75816== 
==75816== 72,704 bytes in 1 blocks are still reachable in loss record 2 of 2
==75816==    at 0x4C29087: malloc (vg_replace_malloc.c:380)
==75816==    by 0x4EC226F: pool (eh_alloc.cc:117)
==75816==    by 0x4EC226F: __static_initialization_and_destruction_0 (eh_alloc.cc:244)
==75816==    by 0x4EC226F: _GLOBAL__sub_I_eh_alloc.cc (eh_alloc.cc:307)
==75816==    by 0x400F1E2: _dl_init (in /usr/lib64/ld-2.17.so)
==75816==    by 0x4001219: ??? (in /usr/lib64/ld-2.17.so)
==75816== 
==75816== LEAK SUMMARY:
==75816==    definitely lost: 0 bytes in 0 blocks
==75816==    indirectly lost: 0 bytes in 0 blocks
==75816==      possibly lost: 0 bytes in 0 blocks
==75816==    still reachable: 72,708 bytes in 2 blocks
==75816==         suppressed: 0 bytes in 0 blocks
==75816== 
==75816== For lists of detected and suppressed errors, rerun with: -s
==75816== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)


原文链接:https://blog.csdn.net/leonardohaig/article/details/87557055

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_43841155/article/details/116591698