推荐使用工具valgrind,安装:

sudo apt install valgrind #debian/ubuntu

内存泄漏示例代码如下:

/* code with memory leak */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{ int *ptr = (int*)malloc(10); return 0;
}

进行检测:

-> # valgrind --leak-check=full ./test
==14896== Memcheck, a memory error detector
==14896== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==14896== Using Valgrind-3.12.0.SVN and LibVEX; rerun with -h for copyright info
==14896== Command: ./test
==14896==
==14896==
==14896== HEAP SUMMARY:
==14896==     in use at exit: 10 bytes in 1 blocks
==14896==   total heap usage: 2 allocs, 1 frees, 72,714 bytes allocated
==14896==
==14896== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1
==14896==    at 0x4C2BBAF: malloc (vg_replace_malloc.c:299)
==14896==    by 0x4004F3: main (test.cpp:6)
==14896==
==14896== LEAK SUMMARY:
==14896==    definitely lost: 10 bytes in 1 blocks
==14896==    indirectly lost: 0 bytes in 0 blocks
==14896==      possibly lost: 0 bytes in 0 blocks
==14896==    still reachable: 0 bytes in 0 blocks
==14896==         suppressed: 0 bytes in 0 blocks
==14896==
==14896== For counts of detected and suppressed errors, rerun with: -v
==14896== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

windows上的话推荐使用CRT:Find memory leaks with the CRT library

参考:How to deallocate memory without using free() in C?


另外一个推荐使用的工具是AddressSanitizer,
使用方法参考博客:LeetCode 报错解决 heap-buffer-overflow Heap-use-after-free Stack-buffer-overflow Global-buffer-overflow
示例代码:

class A {int a;
};class B : public A {int b;
};int main() {A *a = new B();delete a;return 0;
}

上面的代码会造成内存泄漏:

-> % g++ -fsanitize=address test.cpp
-> % ./a.out
=================================================================
==25445==ERROR: AddressSanitizer: new-delete-type-mismatch on 0x602000000010 in thread T0:object passed to delete has wrong type:size of the allocated type:   8 bytes;size of the deallocated type: 4 bytes.#0 0x7f901bc429d8 in operator delete(void*, unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe19d8)#1 0x562ecf25d92c in main (/home/ubuntu/a.out+0x92c)#2 0x7f901b791b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)#3 0x562ecf25d7a9 in _start (/home/ubuntu/a.out+0x7a9)0x602000000010 is located 0 bytes inside of 8-byte region [0x602000000010,0x602000000018)
allocated by thread T0 here:#0 0x7f901bc41458 in operator new(unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0458)#1 0x562ecf25d89b in main (/home/ubuntu/a.out+0x89b)#2 0x7f901b791b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)SUMMARY: AddressSanitizer: new-delete-type-mismatch (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe19d8) in operator delete(void*, unsigned long)
==25445==HINT: if you don't care about these errors you may set ASAN_OPTIONS=new_delete_type_mismatch=0
==25445==ABORTING

报错中说明是new-delete-type-mismatch,分配了8字节,结果只删除了4字节,明显的内存泄漏。

C++ 内存泄漏检测:valgrind和AddressSanitizer相关推荐

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

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

  2. Valgrind ---内存调试,内存泄漏检测以及性能分析的软件开发工具

    Valgrind是一款用于内存调试.内存泄漏检测以及性能分析的软件开发工具.Valgrind这个名字取自北欧神话中英灵殿的入口. 一般使用方式  valgrind --leak-check=full ...

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

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

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

    C++ 内存泄漏检测工具valgrind简单使用 目录 C++ 内存泄漏检测工具valgrind简单使用 valgrind安装 valgrind测试内存泄漏 valgrind安装 通过软件商店下载: ...

  5. 内存泄漏检测神器valgrind

    title: 内存泄漏检测神器valgrind categories:[内存检测] tags:[valgrind] date: 2022/01/08 作者:hackett 微信公众号:加班猿 1.概述 ...

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

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

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

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

  8. Linux C/C++ 内存泄漏检测工具:Valgrind - 张宴的博客 - Web系统架构与底层研发

    Linux C/C++ 内存泄漏检测工具:Valgrind - 张宴的博客 - Web系统架构与底层研发 Linux C/C++ 内存泄漏检测工具:Valgrind - 张宴的博客 - Web系统架构 ...

  9. 内存泄漏检测工具(转载)

    内存泄漏检测工具2007年08月08日 1.     ccmalloc-Linux和Solaris下对C和C++程序的简单的使用内存泄漏和malloc调试库. 2.     Dmalloc-Debug ...

  10. C++ 程序内存泄漏检测方法

    一.前言 在Linux平台上有valgrind可以非常方便的帮助我们定位内存泄漏,因为Linux在开发领域的使用场景大多是跑服务器,再加上它的开源属性,相对而言,处理问题容易形成"统一&qu ...

最新文章

  1. 如何解决问题:程序无法正常启动(0xc0000022)
  2. python 类的知识点整理_Python基础知识点整理,看下你能答对几个
  3. Learning to Rank:X-wise
  4. 51nod 1258 序列求和 V4
  5. Android自动伸展动画,android – 如何实现平滑的展开/折叠动画
  6. qt linux 添加库文件路径,Linux下Qt调用共享库文件.so
  7. 视频教程--ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库
  8. 10分钟 在linux里创建.net core helloworld控制台程序
  9. ​【月报】Java知音的三月汇总
  10. 微软Power BI报表服务器学习总览
  11. oracle中触发器的语法,Oracle 触发器语法及实例
  12. nginx如何编译安装mysql_centos系统编译安装nginx+php环境另加独立mysql教程
  13. Nginx在嵌入式系统中的应用
  14. leetcode—16.两数相加链表python解答
  15. idea中创建jsp项目详细步骤
  16. web的UI自动化实现步骤
  17. StyleAI:色调、感情色彩量化、色彩交流API-PCCS颜色体系
  18. java输出数字格式_java中导出excel设置单元格的样式为数字格式怎样设置?
  19. 数据库原理 头歌实训 数据库常用对象
  20. 关于mysql答辩的问题_答辩常见问题总结.doc

热门文章

  1. Anaconda | CentOS7 -解决 Python2和Python3共存
  2. 数据科学 | Python数据科学常用库
  3. python环境配置是什么意思_python环境搭建
  4. 吐槽 | 聊一聊公众号的那些事
  5. 在线作图|在线做扩增子抽平
  6. Nature子刊:加州大学伯克利分校Banfield组发现某些淡水湖泊中的大噬菌体或能加速好氧甲烷氧化...
  7. ISME:中国林科院亚林所袁志林组揭示盐碱地根系深色有隔内生真菌种群基因岛的正向选择机制...
  8. 本年扩增子、宏基因组课程报名已满,想要学最早等明年
  9. 一作解读Microbiome:所谓的“富集培养”获得的微生物真的都是被“富集”出来的吗?
  10. 判断三角形java代码_小猿圈Java循环嵌套语法的使用介绍