美文网首页C++ceph总结
linux平台代码内存泄露检查

linux平台代码内存泄露检查

作者: 呆呆的张先生 | 来源:发表于2018-08-12 23:21 被阅读8次

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)
...

相关文章

  • linux平台代码内存泄露检查

    linux内存泄露检测cppcheck 静态检查mtracevalgrind linux内存泄露检测 cppche...

  • iOS 内存泄露BUG列举

    1.检查是否可以使用内购 使用Leak 内存泄露工具检查可以看到有内存泄露 2.获取本机DNS 产生内存泄露的代码...

  • 性能检测报告Leaks

    Leaks 内存使用, 检查内存泄露情况 MOBFJSContext

  • 检查内存泄露

    今天看了唐巧大神的理解 iOS 的内存管理这篇博文,介绍了引用计数和ARC下易引起的循环引用问题,并介绍了利用xc...

  • 内存管理

    怎么保证多人开发进行内存泄露的检查?使用Analyze进行代码的静态分析。尽量使用ARC环境开发。 非自动内存管理...

  • Instruments 检测内存泄漏

    目的:检测项目内存泄漏、定位内存泄露代码。 工具:Instruments-->Leaks ⚠️分析内存泄露不能把所...

  • Lua内存泄露检查

    lua中支持垃圾回收机制的对象有五种:string,table,function,full userdata,th...

  • Android内存泄露详解

    内存泄露 在开发应用的过程中,我们总会遇到内存泄露的问题。现在通过代码列出一些常见的内存泄露的情况以及解决方案。 ...

  • iOS面试题02-内存管理(★★★)

    《2018 iOS面试题系列》 一、怎么保证多人开发进行内存泄露的检查. 使用Analyze进行代码的静态分析 为...

  • ios排除内存泄露的方案和修复

    最近APP要求优化,所以内存泄露的检查自然必不可少 1.0 APP添加以下代码解决了因闭包产生循环引用导致的内存泄...

网友评论

    本文标题:linux平台代码内存泄露检查

    本文链接:https://www.haomeiwen.com/subject/cuwebftx.html