C++ 内存泄漏检查工具

Linux

  1. valgrind 工具(官网)
# 安装
[root@localhost] wget https://sourceware.org/pub/valgrind/valgrind-3.15.0.tar.bz2
[root@localhost] tar  jxf valgrind-3.15.0.tar.bz2
[root@localhost] cd   valgrind-3.15.0 && ls
[root@localhost] ./autogen.sh
[root@localhost] ./configure
[root@localhost] make -j4
[root@localhost] make install

[栗子]:

g++ test_mem.cpp  -o  test_mem
// test_mem.cpp  
#include <iostream>
using namespace std;
int main()
{
	int *a = new int(2);
	//delete a;
	return 0;
}

  1. 用法:
    valgrind [options] prog-and-args [options]
-tool=<name  最常用的选项> 运行 valgrind中名为toolname的工具
	1.Memcheck(默认)。一个重量级的内存检查器,能够发现开发中绝大多数内存错误使用情况,比如:使用未初始化的内存,使用已经释放了的内存,内存访问越界等。这也是本文将重点介绍的部分。
	2.Callgrind。它主要用来检查程序中函数调用过程中出现的问题。
	3.Cachegrind。它主要用来检查程序中缓存使用出现的问题。
	4.Helgrind。它主要用来检查多线程程序中出现的竞争问题。
	5.Massif。它主要用来检查程序中堆栈使用中出现的问题。
	6.Extension。可以利用core提供的功能,自己编写特定的内存调试工具

h –help 显示帮助信息。
-version 显示valgrind内核的版本,每个工具都有各自的版本。
q –quiet 安静地运行,只打印错误信息。
v –verbose 更详细的信息, 增加错误数统计。

-trace-children=no|yes 跟踪子线程? [no]
-track-fds=no|yes 跟踪打开的文件描述?[no]
-time-stamp=no|yes 增加时间戳到LOG信息? [no]

LOG信息输出
-log-fd=<number> 输出LOG到描述符文件 [2=stderr]
-log-file=<file> 将输出的信息写入到filename.PID的文件里,PID是运行程序的进行ID
-log-file-exactly=<file> 输出LOG信息到 file
-log-file-qualifier=<VAR> 取得环境变量的值来做为输出信息的文件名。 [none]
-log-socket=ipaddr:port 输出LOG到socket ,ipaddr:port

-xml=yes 将信息以xml格式输出,只有memcheck可用
-num-callers=<number> show <number> callers in stack traces [12]
-error-limit=no|yes 如果太多错误,则停止显示新错误? [yes]
-error-exitcode=<number> 如果发现错误则返回错误代码 [0=disable]
-db-attach=no|yes 当出现错误,valgrind会自动启动调试器gdb。[no]
-db-command=<command> 启动调试器的命令行选项[gdb -nw %f %p]


适用于Memcheck工具的相关选项:
-leak-check=no|summary|full 要求对leak给出详细信息? [summary]
-leak-resolution=low|med|high how much bt merging in leak check [low]
-show-reachable=no|yes show reachable blocks in leak check? [no]
valgrind --tool=memcheck --leak-check=full  ./test_mem
# 输出
==16066== Memcheck, a memory error detector
==16066== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==16066== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==16066== Command: ./test_mem
==16066==
==16066==
==16066== HEAP SUMMARY:
==16066==     in use at exit: 4 bytes in 1 blocks
==16066==   total heap usage: 1 allocs, 0 frees, 4 bytes allocated
==16066==
==16066== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==16066==    at 0x4C2A4C3: operator new(unsigned long) (vg_replace_malloc.c:344)
==16066==    by 0x40069E: test_memory() (in ./test_mem)
==16066==    by 0x4006B3: main (in ./test_mem)
==16066==
==16066== LEAK SUMMARY:
==16066==    definitely lost: 4 bytes in 1 blocks
==16066==    indirectly lost: 0 bytes in 0 blocks
==16066==      possibly lost: 0 bytes in 0 blocks
==16066==    still reachable: 0 bytes in 0 blocks
==16066==         suppressed: 0 bytes in 0 blocks
==16066==
==16066== For lists of detected and suppressed errors, rerun with: -s
==16066== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

#------------------------- 解释 ------------------------
definitely lost: #确定的内存泄露: 已经不能够访问这块内存, 分为:
				                      #直接: 没有任何指针指向该内存
						              #间接: 指向该内存的指针都位于内存泄露处
possibly   lost: #可能的内存泄露
#函数调用栈指出: main 中的 test_memory() 中调用了 new 操作符 导致泄漏了 4 字节的内存
==16066== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==16066==    at 0x4C2A4C3: operator new(unsigned long) (vg_replace_malloc.c:344)
==16066==    by 0x40069E: test_memory() (in ./test_mem)
==16066==    by 0x4006B3: main (in ./test_mem)

#------------------------- 常见英文解释 ------------------------
40 bytes in 1 blocks are definitely lost in loss record 1 of 1   #内存没有释放
Invalid write of size 4                          #可能是数组越界了
Use of uninitialised memory                      #使用未初始化的内存
Reading/writing memory after it has been free’d  #使用已经释放了的内存
Reading/writing off the end of malloc’d blocks   #使用超过malloc分配的内存空间
Reading/writing inappropriate areas on the stack #对堆栈的非法访问
Memory leaks – where pointers to malloc’d blocks are lost forever  #申请的空间是否有释放
Mismatched use of malloc/new/new [] vs free/delete/delete []   #malloc/free/new/delete申请和释放内存的匹配
Overlapping src and dst pointers in memcpy() and related functions  #src和dst的重叠
重复free

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值