如何使用Valgrind memcheck工具进行C/C++的内存泄漏检测

英文原文:How to Detect Memory Leaks Using Valgrind memcheck Tool for C / C++


系统编程中一个重要的方面就是有效地处理与内存相关的问题。你的工作越接近系统,你就需要面对越多的内存问题。有时这些问题非常琐碎,而更多时候它会演变成一个调试内存问题的恶梦。所以,在实践中会用到很多工具来调试内存问题。

在本文中,我们将讨论最流行的开源内存管理框架 VALGRIND。

摘自 Valgrind.org:

Valgrind是用于构建动态分析工具的探测框架。它包括一个工具集,每个工具执行某种类型的调试、分析或类似的任务,以帮助完善你的程序。Valgrind的架构是模块化的,所以可以容易地创建新的工具而又不会扰乱现有的结构。

 

许多有用的工具被作为标准而提供。

  1. Memcheck是一个内存错误检测器。它有助于使你的程序,尤其是那些用C和C++写的程序,更加准确。
  2. Cachegrind是一个缓存和分支预测分析器。它有助于使你的程序运行更快。
  3. Callgrind是一个调用图缓存生成分析器。它与Cachegrind的功能有重叠,但也收集Cachegrind不收集的一些信息。
  4. Helgrind是一个线程错误检测器。它有助于使你的多线程程序更加准确。
  5. DRD也是一个线程错误检测器。它和Helgrind相似,但使用不同的分析技术,所以可能找到不同的问题。
  6. Massif是一个堆分析器。它有助于使你的程序使用更少的内存。
  7. DHAT是另一种不同的堆分析器。它有助于理解块的生命期、块的使用和布局的低效等问题。
  8. SGcheck是一个实验工具,用来检测堆和全局数组的溢出。它的功能和Memcheck互补:SGcheck找到Memcheck无法找到的问题,反之亦然。
  9. BBV是个实验性质的SimPoint基本块矢量生成器。它对于进行计算机架构的研究和开发很有用处。

也有一些对大多数用户没有用的小工具:Lackey是演示仪器基础的示例工具;Nulgrind是一个最小化的Valgrind工具,不做分析或者操作,仅用于测试目的。

在这篇文章我们将关注“memcheck”工具。

 

使用 Valgrind Memcheck

memcheck工具的使用方式如下:

?
1
valgrind --tool=memcheck ./a.out

从上面的命令可以清楚的看到, 主要的命令是valgrind,而我们想使用的工具是通过'-tool'选项来指定的. 上面的‘a.out’指的是我们想使用memcheck运行的可执行文件.

该工具可以检测下列与内存相关的问题 :

  • 未释放内存的使用
  • 对释放后内存的读/写
  • 对已分配内存块尾部的读/写
  • 内存泄露
  • 不匹配的使用malloc/new/new[] 和 free/delete/delete[]
  • 重复释放内存

注意: 上面列出的并不很全面,但却包含了能被该工具检测到的很多普遍的问题.

让我们一个一个地对上面的场景进行讨论:

注意: 下面讨论的所有测试代码都应该使用gcc并且加上-g选项(用来在memcheck的输出中生成行号)进行编译. 就想我们之前讨论过的 C程序被编译成可执行文件, 它需要经历四个不同的阶段.

 

1. 使用未初始化的内存

Code :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    char *p;
    char c = *p;
    printf("\n [%c]\n",c);
    return 0;
}

在上面的代码中,我们尝试使用未初始化的指针 ‘p’.

让我们运行Memcheck来看下结果.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ valgrind --tool=memcheck ./val
==2862== Memcheck, a memory error detector
==2862== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==2862== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==2862== Command: ./val
==2862==
==2862== Use of uninitialised value of size 8
==2862==    at 0x400530: main (valgrind.c:8)
==2862==
[#]
==2862==
==2862== HEAP SUMMARY:
==2862==     in use at exit: 0 bytes in 0 blocks
==2862==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==2862==
==2862== All heap blocks were freed -- no leaks are possible
==2862==
==2862== For counts of detected and suppressed errors, rerun with: -v
==2862== Use --track-origins=yes to see where uninitialized values come from
==2862== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)

从上面的输出可以看到,Valgrind检测到了未初始化的变量,然后给出了警告(上面加粗的几行(译者注:貌似上面没有加粗的)).

2. 在内存被释放后进行读/写

Code :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    char *p = malloc(1);
    *p = 'a';
    char c = *p;
    printf("\n [%c]\n",c);
    free(p);
    c = *p;
    return 0;
}

上面的代码中,我们有一个释放了内存的指针 ‘p’ 然后我们又尝试利用指针获取值.

让我们运行memcheck来看一下Valgrind对这种情况是如何反应的.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ valgrind --tool=memcheck ./val
==2849== Memcheck, a memory error detector
==2849== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==2849== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==2849== Command: ./val
==2849==
 [a]
==2849== Invalid read of size 1
==2849==    at 0x400603: main (valgrind.c:30)
==2849==  Address 0x51b0040 is 0 bytes inside a block of size 1 free'd
==2849==    at 0x4C270BD: free (vg_replace_malloc.c:366)
==2849==    by 0x4005FE: main (valgrind.c:29)
==2849==
==2849==
==2849== HEAP SUMMARY:
==2849==     in use at exit: 0 bytes in 0 blocks
==2849==   total heap usage: 1 allocs, 1 frees, 1 bytes allocated
==2849==
==2849== All heap blocks were freed -- no leaks are possible
==2849==
==2849== For counts of detected and suppressed errors, rerun with: -v
==2849== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)

从上面的输出内容可以看到,Valgrind检测到了无效的读取操作然后输出了警告 ‘Invalid read of size 1′.

另注,使用gdb来调试c程序.

3. 从已分配内存块的尾部进行读/写

Code :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    char *p = malloc(1);
    *p = 'a';
    char c = *(p+1);
    printf("\n [%c]\n",c);
    free(p);
    return 0;
}

在上面的代码中,我们已经为‘p’分配了一个字节的内存,但我们在将值读取到 ‘c’中的时候使用的是地址p+1.

现在我们使用Valgrind运行上面的代码 :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ valgrind --tool=memcheck ./val
==2835== Memcheck, a memory error detector
==2835== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==2835== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==2835== Command: ./val
==2835==
==2835== Invalid read of size 1
==2835==    at 0x4005D9: main (valgrind.c:25)
==2835==  Address 0x51b0041 is 0 bytes after a block of size 1 alloc'd
==2835==    at 0x4C274A8: malloc (vg_replace_malloc.c:236)
==2835==    by 0x4005C5: main (valgrind.c:22)
==2835==
 []
==2835==
==2835== HEAP SUMMARY:
==2835==     in use at exit: 0 bytes in 0 blocks
==2835==   total heap usage: 1 allocs, 1 frees, 1 bytes allocated
==2835==
==2835== All heap blocks were freed -- no leaks are possible
==2835==
==2835== For counts of detected and suppressed errors, rerun with: -v
==2835== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)

同样,该工具在这种情况下也检测到了无效的读取操作.

4. 内存泄露

Code:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    char *p = malloc(1);
    *p = 'a';
    char c = *p;
    printf("\n [%c]\n",c);
    return 0;
}

在这次的代码中, 我们申请了一个字节但是没有将它释放.现在让我们运行Valgrind看看会发生什么:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ valgrind --tool=memcheck --leak-check=full ./val
==2888== Memcheck, a memory error detector
==2888== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==2888== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==2888== Command: ./val
==2888==
 [a]
==2888==
==2888== HEAP SUMMARY:
==2888==     in use at exit: 1 bytes in 1 blocks
==2888==   total heap usage: 1 allocs, 0 frees, 1 bytes allocated
==2888==
==2888== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1
==2888==    at 0x4C274A8: malloc (vg_replace_malloc.c:236)
==2888==    by 0x400575: main (valgrind.c:6)
==2888==
==2888== LEAK SUMMARY:
==2888==    definitely lost: 1 bytes in 1 blocks
==2888==    indirectly lost: 0 bytes in 0 blocks
==2888==      possibly lost: 0 bytes in 0 blocks
==2888==    still reachable: 0 bytes in 0 blocks
==2888==         suppressed: 0 bytes in 0 blocks
==2888==
==2888== For counts of detected and suppressed errors, rerun with: -v
==2888== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)

输出行(上面加粗的部分)显示,该工具能够检测到内存的泄露.

注意: 在这里我们增加了一个选项‘–leak-check=full’来得到内存泄露的详细细节.

5. 不匹配地使用malloc/new/new[] 和 free/delete/delete[]

Code:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
int main(void)
{
    char *p = (char*)malloc(1);
    *p = 'a';
    char c = *p;
    printf("\n [%c]\n",c);
    delete p;
    return 0;
}

上面的代码中,我们使用了malloc()来分配内存,但是使用了delete操作符来删除内存.

注意 : 使用g++来编译上面的代码,因为delete操作符是在C++中引进的,而要编译C++需要使用g++.

让我们运行来看一下 :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ valgrind --tool=memcheck --leak-check=full ./val
==2972== Memcheck, a memory error detector
==2972== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==2972== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==2972== Command: ./val
==2972==
 [a]
==2972== Mismatched free() / delete / delete []
==2972==    at 0x4C26DCF: operator delete(void*) (vg_replace_malloc.c:387)
==2972==    by 0x40080B: main (valgrind.c:13)
==2972==  Address 0x595e040 is 0 bytes inside a block of size 1 alloc'd
==2972==    at 0x4C274A8: malloc (vg_replace_malloc.c:236)
==2972==    by 0x4007D5: main (valgrind.c:7)
==2972==
==2972==
==2972== HEAP SUMMARY:
==2972==     in use at exit: 0 bytes in 0 blocks
==2972==   total heap usage: 1 allocs, 1 frees, 1 bytes allocated
==2972==
==2972== All heap blocks were freed -- no leaks are possible
==2972==
==2972== For counts of detected and suppressed errors, rerun with: -v
==2972== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)

从上面的输出可以看到 (加粗的行), Valgrind清楚的说明了‘不匹配的使用了free() / delete / delete []‘

你可以尝试在测试代码中使用'new'和'free'进行组合来看看Valgrind给出的结果是什么.

 

6. 两次释放内存

Code :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    char *p = (char*)malloc(1);
    *p = 'a';
    char c = *p;
    printf("\n [%c]\n",c);
    free(p);
    free(p);
    return 0;
}

在上面的代码中, 我们两次释放了'p'指向的内存. 现在让我们运行memcheck :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ valgrind --tool=memcheck --leak-check=full ./val
==3167== Memcheck, a memory error detector
==3167== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==3167== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==3167== Command: ./val
==3167==
 [a]
==3167== Invalid free() / delete / delete[]
==3167==    at 0x4C270BD: free (vg_replace_malloc.c:366)
==3167==    by 0x40060A: main (valgrind.c:12)
==3167==  Address 0x51b0040 is 0 bytes inside a block of size 1 free'd
==3167==    at 0x4C270BD: free (vg_replace_malloc.c:366)
==3167==    by 0x4005FE: main (valgrind.c:11)
==3167==
==3167==
==3167== HEAP SUMMARY:
==3167==     in use at exit: 0 bytes in 0 blocks
==3167==   total heap usage: 1 allocs, 2 frees, 1 bytes allocated
==3167==
==3167== All heap blocks were freed -- no leaks are possible
==3167==
==3167== For counts of detected and suppressed errors, rerun with: -v
==3167== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)

从上面的输出可以看到(加粗的行), 该功能检测到我们对同一个指针调用了两次释放内存操作.

在本文中,我们把注意力放在了内存管理框架Valgrind,然后使用memcheck(Valgrind框架提供的)工具来了解它是如何降低需要经常操作内存的程序员的负担的. 该工具能够检测到很多手动检测不到的与内存相关的问题

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/352248.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何在Activiti中使用瞬态变量

我们昨天发布的Activiti v6 Beta3中已经加入了一个非常需要的功能-临时变量。 在本文中&#xff0c;我将向您展示一个示例&#xff0c;该示例说明如何使用瞬态变量来覆盖一些以前不可能&#xff08;或最佳&#xff09;的高级用例。 到目前为止&#xff0c;Activiti中的所有变量…

python羊车门问题_「羊车门」经典概率题中不换门选中车的概率是多少?

今天用Python求解「羊车门」经典的概率问题,对概率学基础和Python语法的灵活运用有所收货.本次「羊车门」求解过程采用的是:穷举法计算概率已验证概率学基础理论.期间重点借鉴了奥卡姆剃刀的博客和 南葱&#xff1a;「羊车门」经典概率题中不换门选中车的概率是多少&#xff1f…

非标协议外设LCD1602

概述 LCD1602 &#xff08; Liquid Crystal Display &#xff09;是一种工业字符型液晶&#xff0c;能够同时显示 1602 即 32 字符 (16 列两行) 引脚说明 第 1 脚 : VSS 为电源地 第 2 脚 : VDD 接 5V 正电源 第 3 脚 : VL 为液晶显示器对比度调整端 , 接正电源…

50: Luogu P4568 分层图

分层图最短路模板 #include <iostream> #include <cstdio> #include <cstdlib> #include <ctime> #include <queue> #include <cstring>using namespace std;const int M 2e6 5e5 10;#define gc getchar() inline int read() {int x 0…

C++编程笔记:dll的生成与使用

1.动态链接库&#xff08;dll&#xff09;概述 没接触dll之前觉得它很神秘&#xff0c;就像是一个黑盒子&#xff0c;既不能直接运行&#xff0c;也不能接收消息。它们是一些独立的文件&#xff0c;其中包含能被可执行程序或其他dll调用来完成某项工作的函数&#xff0c;只有在…

如何通过IP地址分辨公网、私网、内网、外网

如何通过IP地址分辨公网、私网、内网、外网内、外网是相对于防火墙而言的&#xff0c;在防火墙内部叫做内网&#xff0c;反之就是外网。在一定程度上外网等同于公网&#xff0c;内网等同于私网。地址为如下3个区域就是处于私网&#xff1a;1&#xff1a;10.*.*.*2&#xff1a;1…

python画动态表情包_真香!一行Python代码,帮你制作小姐姐的表情包,靠谱吗?...

原标题&#xff1a;真香&#xff01;一行Python代码&#xff0c;帮你制作小姐姐的表情包&#xff0c;靠谱吗&#xff1f;(我的IU女神)对于小姐姐的动态表情包&#xff0c;相必我们大多数人都不会拒绝&#xff0c;而且都会选择默默的将其收藏(不要问我怎么知道的)&#xff0c;一…

mongodb分片

mongodb分片&#xff1a; 本次是用三台主机搭建3个集群&#xff08;主、备、仲裁&#xff09;作为三个分片&#xff0c;一个集群&#xff08;主、备、备&#xff09;做为config服务器&#xff0c;三个mongos单点做路由&#xff0c;每台5个&#xff0c;一共15个。 新建一个mongo…

批量添加PDF帐号目录

本文参考&#xff1a;https://blog.csdn.net/qq_34104395/article/details/78766400然后根据需要整理的。如作者介意请留言&#xff0c;本人会尽快处理&#xff01; 准备材料&#xff1a; 下载工具FreePic2Pdf&#xff08;在本博客上传资料上找PDF转换工具包&#xff09; 找到…

驳斥5条普通流Tropes

我刚读完“ JDK 8收集器的强大功能的一种例外” &#xff0c;我不得不说我很失望。 Java冠军 Simon Ritter是Oracle的前Java推广者&#xff0c;现在是Oracle的Java传播者&#xff0c;现在是Azul Systems的副CTO&#xff08;使用JVM的人 &#xff09;写了它&#xff0c;因此我希…

私网IP如何访问Internet

公网、内网是两种Internet的接入方式。 内网接入方式&#xff1a;上网的计算机得到的IP地址是Inetnet上的保留地址&#xff0c;保留地址有如下3种形式&#xff1a; 10.x.x.x 172.16.x.x至172.31.x.x 192.168.x.x 内网的计算机以NAT&#xff08;网络地址转换&#xf…

钉钉机器人发送图片 python_python封装钉钉Webhook机器人消息发送逻辑

python封装钉钉Webhook机器人消息发送逻辑&#xff0c;目前仅支持python2。安装pip install dingmsgapi初始化实例from ding_msg_api import MsgClient# Webhook机器人access_tokenmsgClient MsgClient("****************")发送Text消息from ding_msg_api import Te…

[LevelDB] 写批处理过程详解

leveldb的write代码初看瞎搞一堆&#xff0c;细看则实为短小精悍。1 Status DBImpl::Write(const WriteOptions& options, WriteBatch* my_batch) { 2  // -----A begin------- 3 Writer w(&mutex_); 4 w.batch my_batch; 5 w.sync options.sync; 6 w.d…

关于excel vba 使用CopyFromRecordset出现格式问题的解决方法

关于excel vba 使用CopyFromRecordset出现格式问题的解决方法 出现问题的写法&#xff1a; With ActiveSheet .Name k(i) For num 1 To UBound(myArray) .Cells(1, num) myArray(num, 1) Next num .rang…

python histo 改变 bins 大小_在Python中显示具有非常不均匀的bin宽度的直方图

这是直方图为了生成这个图,我做了&#xff1a;bins np.array([0.03, 0.3, 2, 100])plt.hist(m, bins bins, weightsnp.zeros_like(m) 1. / m.size)但是,正如您所注意到的,我想绘制每个数据点的相对频率的直方图,只有3个不同大小的区间&#xff1a;bin1 0.03 – > 0.3bin…

parted工具详解

通常我们用的比较多的一般都是fdisk工具来进行分区&#xff0c;但是现在由于磁盘越来越廉价&#xff0c;而且磁盘空间越来越大&#xff1b;而fdisk工具他对分区是有大小限制的&#xff0c;它只能划分小于2T的磁盘。但是现在的磁盘空间很多都已经是远远大于2T了&#xff0c;甚至…

Python安装pyinstaller模块的错误:NO module name “setuptools“

出现改pyinstaller安装错误常见问题是&#xff1a;pip版本或者setuptools包版本过低。 出现上图提示的错误后&#xff0c;升级一下setuptools包&#xff1a; &#xff08;1&#xff09;pip install --upgrade setuptools &#xff08;2&#xff09;pip install pyinstaller

jvm ide_预热JVM –超快速生产服务器和IDE

jvm ide几个月前&#xff0c;我正在阅读Java中的复杂事件处理以及实现低延迟的方法。 在我长达一个小时的研究结束时&#xff0c;我发现即使您的应用程序编写正确并且您的方法主要在0&#xff08;log n&#xff09;的时间内运行&#xff0c;并且您使用的是某些尖端的硬件解决方…

Python 项目打包成可执行程序命令

一、安装pyinstaller (1)winR输入cmd&#xff0c;打开命令窗口 2&#xff09;安装pyinstaller&#xff0c;安装指令&#xff1a;pip install pyinstaller 二、打包 1&#xff0c;切换到打包程序目录 例&#xff1a;需要打包程序目录为&#xff1a;D:\pythonfun\useringfunct…

软RAID-mdadm折腾小记

RAID --- 磁盘阵列,简言之,用来提高硬盘的利用率和速度RAID种类(理论):RAID 0 : 读写性能(最少两块硬盘) --- 硬盘使用量是所有硬盘大小之和,性能是所有硬盘之和RAID 1 : 读写性能,冗余性(最少两块硬盘) --- 空间利用率:所有磁盘中最小的那块(n/2); 读性能接近RAID0,写性能较r…