第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

linux平臺(tái)代碼內(nèi)存泄露檢查

標(biāo)簽:
Linux

linux内存泄露检测

// file        : demo.cpp// description : show the memmory leak detectionint 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)
...



作者:呆呆的张先生
链接:https://www.jianshu.com/p/cac5d5beadb8


點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
JAVA開(kāi)發(fā)工程師
手記
粉絲
205
獲贊與收藏
1011

關(guān)注作者,訂閱最新文章

閱讀免費(fèi)教程

  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消