linux内存泄露检测
// file : demo.cpp
// description : show the memmory leak detection
int main()
{
int* a = new int[10]; // 1, new and not delete
a[10] = 1; // 2, array over range
}
cppcheck 静态检查
可以选择 vscode 安装 cppcheck 插件
zhanghl@zhanghl-Inspiron-7460:~/workspace/env/05_valgrind$ cppcheck demo.cpp
Checking demo.cpp...
[demo.cpp:41]: (error) Array 'a[10]' accessed at index 10, which is out of bounds.
[demo.cpp:45]: (error) Memory leak: a
ps. 不能检测太复杂的代码,检测如下 mtrace 的代码, mtrace 等系统调用会干扰检查结果
mtrace
需要在代码中添加
mtrace()
来检查堆的一致性,另外需要设置环境变量MALLOC_TRACE
#include <cstdlib> // setenv
#include <cstring> // strerror
#include <string>
#include <errno.h> // errno
#include <mcheck.h>
#include <iostream>
int main()
{
using std::string;
using std::cerr;
using std::endl;
const string mtraceFl( "mtrace_outfile" );
if( -1 == setenv( "MALLOC_TRACE", mtraceFl.c_str(), 1 ) )
cerr << "Error: set mtrace env( " << strerror( errno ) << " )" << endl;
mtrace();
int* a = new int[10];
a[10] = 1;
muntrace();
}
编译时加入调试选项
-g
和链接库-mcheck
,调用完成调用mtrace
分析输出的数据文件
zhanghl@zhanghl-Inspiron-7460:~/workspace/env/05_valgrind$ g++ -g -o demo demo.cpp -lmcheck
zhanghl@zhanghl-Inspiron-7460:~/workspace/env/05_valgrind$ ./demo
zhanghl@zhanghl-Inspiron-7460:~/workspace/env/05_valgrind$ mtrace mtrace_outfile
Memory not freed:
-----------------
Address Size Caller
0x0000000000698460 0x28 at 0x7fdd0281ce78
ps. 不能检测检查到内存非法访问
valgrind
可以检查出非法访问,但是
mtrace
同样会被认定为内存泄露
zhanghl@zhanghl-Inspiron-7460:~/workspace/env/05_valgrind$ valgrind ./demo
==22941== Memcheck, a memory error detector
==22941== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==22941== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==22941== Command: ./demo
==22941==
==22941== Invalid write of size 4 // 1, 内存非法访问,可以定位到代码
==22941== at 0x400F03: main (demo.cpp:38)
==22941== Address 0x5ab74a8 is 0 bytes after a block of size 40 alloc'd
==22941== at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22941== by 0x400EF6: main (demo.cpp:25)
==22941==
==22941==
==22941== HEAP SUMMARY:
==22941== in use at exit: 73,256 bytes in 3 blocks
==22941== total heap usage: 7 allocs, 4 frees, 74,452 bytes allocated
==22941==
==22941== LEAK SUMMARY:
==22941== definitely lost: 552 bytes in 2 blocks // 2块内存泄露,其中一块是 mtrace() 引入的
==22941== indirectly lost: 0 bytes in 0 blocks
==22941== possibly lost: 0 bytes in 0 blocks
==22941== still reachable: 72,704 bytes in 1 blocks // 3, still rechable 没有关系
==22941== suppressed: 0 bytes in 0 blocks
==22941== Rerun with --leak-check=full to see details of leaked memory
==22941==
==22941== For counts of detected and suppressed errors, rerun with: -v
==22941== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
查看详细信息,
--leak-check=full -v
...
==23742== 40 bytes in 1 blocks are definitely lost in loss record 1 of 3
==23742== at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23742== by 0x400EF6: main (demo.cpp:25)
==23742==
==23742== 512 bytes in 1 blocks are definitely lost in loss record 2 of 3
==23742== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23742== by 0x545AF57: mtrace (mtrace.c:296)
==23742== by 0x400EEC: main (demo.cpp:23)
...
网友评论