C++ 内存泄漏检测工具valgrind简单使用

目录

  • C++ 内存泄漏检测工具valgrind简单使用
    • valgrind安装
    • valgrind测试内存泄漏

valgrind安装

通过软件商店下载:

sudo apt install valgrind
//查看可用参数
valgrind -h
//查看版本
valgrind --version

或者通过源码下载:
valgrind源码下载和安装

valgrind测试内存泄漏

首先准备一个内存泄漏的c++文件:

#include<iostream>
#include<bits/stdc++.h>
using namespace std;int main() {printf("测试内存泄漏, 二维数组为例\n");int **a = new int*[2];for( int i = 0; i < 2; i++ ){a[i]=new int[2]; }
}

编译,然后通过valgrind执行:

root@ubuntu:/home/qiye# g++ -o t133 /mnt/hgfs/c/t133.cpp
root@ubuntu:/home/qiye# valgrind --tool=memcheck --leak-check=full --show-reachable=yes --trace-children=yes ./t133
==85576== Memcheck, a memory error detector
==85576== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==85576== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
==85576== Command: ./t133
==85576==
测试内存泄漏, 二维数组为例
==85576==
==85576== HEAP SUMMARY:
==85576==     in use at exit: 32 bytes in 3 blocks
==85576==   total heap usage: 5 allocs, 2 frees, 73,760 bytes allocated
==85576==
==85576== 16 bytes in 2 blocks are indirectly lost in loss record 1 of 2
==85576==    at 0x4844203: operator new[](unsigned long) (vg_replace_malloc.c:640)
==85576==    by 0x1091FB: main (in /home/qiye/t133)
==85576==
==85576== 32 (16 direct, 16 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2
==85576==    at 0x4844203: operator new[](unsigned long) (vg_replace_malloc.c:640)
==85576==    by 0x1091CB: main (in /home/qiye/t133)
==85576==
==85576== LEAK SUMMARY:
==85576==    definitely lost: 16 bytes in 1 blocks
==85576==    indirectly lost: 16 bytes in 2 blocks
==85576==      possibly lost: 0 bytes in 0 blocks
==85576==    still reachable: 0 bytes in 0 blocks
==85576==         suppressed: 0 bytes in 0 blocks
==85576==
==85576== For lists of detected and suppressed errors, rerun with: -s
==85576== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

可以发现,它明确指出我们没有使用free() / delete / delete []等方式释放内存,导致了内存泄漏,在HEAP SUMMARY,5 allocs, 2 frees,表示分配了5次,却只释放了2次,其中:

  • main()函数和printf()函数各分配了一次(2 allocs),程序结束时,它们会自动释放(2 frees)。
  • 申请长度为2的二维数组和二维数组的2个指针数组申请共分配了3次(3 allocs),但是由于没有使用delete释放,所以没有frees。
  • in use at exit: 32 bytes in 3 blocks, 二维数组内2个指针共占16bytes,for循环申请了2 x 2 x 4(int类型占4个字节) = 16bytes,所以16bytes + 16bytes = 32bytes,definitely lost: 16 bytes in 1 blocks,两个指针都没有被释放,故内存泄漏总共32bytes。

解决内存泄漏:

#include<iostream>
#include<bits/stdc++.h>
using namespace std;int main() {printf("测试内存泄漏, 二维数组为例\n");int **a = new int*[2];for( int i = 0; i < 2; i++ ){a[i]=new int[2]; }//释放内存for ( int i = 0 ; i < 2 ; ++i ){delete[] a[i];}delete[] a;return 0;
}

编译后使用valgrind执行:

root@ubuntu:/home/qiye# g++ -o t133 /mnt/hgfs/c/t133.cpp
root@ubuntu:/home/qiye# valgrind --tool=memcheck --leak-check=full --show-reachable=yes --trace-children=yes ./t133
==68985== Memcheck, a memory error detector
==68985== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==68985== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
==68985== Command: ./t133
==68985==
测试内存泄漏, 二维数组为例
==68985==
==68985== HEAP SUMMARY:
==68985==     in use at exit: 0 bytes in 0 blocks
==68985==   total heap usage: 5 allocs, 5 frees, 73,760 bytes allocated
==68985==
==68985== All heap blocks were freed -- no leaks are possible
==68985==
==68985== For lists of detected and suppressed errors, rerun with: -s
==68985== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

C++ 内存泄漏检测工具valgrind简单使用相关推荐

  1. C/C++的内存泄漏检测工具Valgrind memcheck的使用经历

    Linux下的Valgrind真是利器啊(不知道Valgrind的请自觉查看参考文献(1)(2)),帮我找出了不少C++中的内存管理错误,前一阵子还在纠结为什么VS 2013下运行良好的程序到了Lin ...

  2. Linux内存泄漏检测工具 Valgrind使用

    一 .valgrind简介 最近在Linux下程序碰到了内存泄漏的问题,所以在网上找了找Linux下的内存泄漏检测工具,找到了Valgrind这款功能很强大的内存调试.内存泄漏检测以及性能分析.检测线 ...

  3. 【安装配置】安装适用于 Linux 的 Windows 子系统 WSL ,完成 Clion 中对内存泄漏检测工具 Valgrind 的配置,亲测可用

    关键词:[Linux] [WSL] [Clion] [Valfrind] 一.前言 今天在回答一个粉丝的评论(关于C++ delete 和 delete[ ])时,引出上面的系列问题,具体流程如下: ...

  4. Unix下C程序内存泄漏检测工具Valgrind安装与使用

    Valgrind是一款用于内存调试.内存泄漏检测以及性能分析的软件开发工具. Valgrind的最初作者是Julian Seward,他于2006年由于在开发Valgrind上的工作获得了第二届Goo ...

  5. Linux 内存泄漏检测工具 Valgrind

    Valgrind 是一款 Linux下(支持 x86.x86_64和ppc32)程序的内存调试工具,它可以对编译后的二进制程序进行内存使用监测(C语言中的malloc和free,以及C++中的new和 ...

  6. Linux:内存泄漏检测工具--Valgrind

    在写大型C/C++工程时难免会发生内存泄漏现象,系统编程中一个重要的方面就是有效地处理与内存相关的问题.你的工作越接近系统,你就需要面对越多的内存问题.有时这些问题非常琐碎,而更多时候它会演变成一个调 ...

  7. 内存泄漏检测工具valgrind神器

    1.1 介绍 Valgrind是一套Linux下,开放源代码(GPL V2)的仿真调试工具的集合.Valgrind由内核(core)以及基于内核的其他调试工具组成.内核类似于一个框架(framewor ...

  8. c linux new使内存耗尽_C/C++的内存泄漏检测工具Valgrind memcheck的使用经历

    Linux下的Valgrind真是利器啊(不知道Valgrind的请自觉查看参考文献(1)(2)),帮我找出了不少C++中的内存管理错误,前一阵子还在纠结为什么VS 2013下运行良好的程序到了Lin ...

  9. C++内存泄漏检测工具

    C++内存泄漏检测工具 1.VC自带的CRT:_CrtCheckMemory   调试器和 CRT 调试堆函数 1.1用法: /************************************ ...

最新文章

  1. 二 IOC之PropertyPlaceholderConfigurer
  2. linux centos7 yum 报错 Public key for *.rpm is not installed 解决方法
  3. php中延迟绑定,PHP静态延迟绑定
  4. 程序员的失业危机原因及应对方法汇总
  5. iOS利用cocoapods 和GitHub组件化序Day1
  6. LDAP密码认证例子
  7. 实战Node:Node实现留言板
  8. 【python|opencv】cv2.imread返回None,无法正确读取图片
  9. php存库,php – 使用Laravel实现存储库模式
  10. NC6 转库单、采购入库单、库存委托入库单签字后自动生成调拨订单
  11. mysql proxy maxscale_maxscale参数配置
  12. 技术是如何创造价值的
  13. 百度api验证码识别
  14. nRF52832 BLE UART
  15. 在线客服功能介绍-了解常见在线客服系统的功能点
  16. 1-1/2+1/3-1/4+.........+1/99-1/100
  17. PHP中一些通用和易混淆技术点的最佳编程实践
  18. 计算机word实线边框在哪,4.22 Word 2016 形状轮廓(边框)设置(实线、渐变线)-Word2016入门进阶教程-亮术网...
  19. 2021年11月推荐阅读的10篇论文
  20. 来自日本的品质传承 工机控股发布新品牌HiKOKI战略

热门文章

  1. 在java中使用JDBC连接mysql数据库时的服务器时区值无法识别或表示多个时区的问题解决方案
  2. 计算机网络中常见的各层协议
  3. http chunked问题记录
  4. 解决win10系统搜索框无法使用
  5. Apollo简单介绍
  6. 同步与异步通信的区别
  7. sql优化(面试必问一)
  8. STM32——串口概念及应用
  9. python IDLE设置了清屏插件,不起作用怎么办
  10. centos7免密登录