看板 LinuxDev 關於我們 聯絡資訊
我照著q大提供的連結試用了valgrind 因為我用的是Ubuntu 所以是直接上repository下載的版本 (valgrind-3.2.0-Debian) 用法跟gdb差不多 很直覺 查memeory leak最簡單的方法就是用 % valgrind --tool=memcheck program_name 如果一切正常沒有漏記憶體 會有如下的畫面: ==22574== Memcheck, a memory error detector. ==22574== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al. ==22574== Using LibVEX rev 1606, a library for dynamic binary translation. ==22574== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP. ==22574== Using valgrind-3.2.0-Debian, a dynamic binary instrumentation framework. ==22574== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al. ==22574== For more details, rerun with: -v ==22574== ==22574== ==22574== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 31 from 1) ==22574== malloc/free: in use at exit: 0 bytes in 0 blocks. ==22574== malloc/free: 10,319 allocs, 10,319 frees, 156,300 bytes allocated. ==22574== For counts of detected errors, rerun with: -v ==22574== All heap blocks were freed -- no leaks are possible. 除了檢查漏記憶體之外 他也會找一些看起來不太對勁的語法 比如我之前有一個用來去除字串前後空白的函式 裏面用到這樣的語法 if(i) strcpy(string,string+i); (這種東西碰到string是字串常數就會segfault 只是之前偷懶不去管) 他就警告我source跟destination都一樣之類的 嚴格的程度比gcc -Wall更高 另外像C的automatic variable如果宣告太大的陣列 (像char buffer[1024*1024] 之類的東西) 有些位址好像會overlap 運氣不好可能就會當的亂七八糟 gcc不會管 但他也會告訴你 總之 目前用的挺滿意 供大家參考囉! -- We are Microsoft. Unix is irrelevant. Openness is futile. Prepare to be assimilated. -- seen on slashdot.org -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.109.176.158 ※ 編輯: dozer 來自: 140.109.176.158 (12/15 18:03)
qrtt1:呵..建議標題-> memory leak test tool: valgrind 12/15 20:33
qrtt1:比較方便爬文的人search 12/15 20:33
※ 編輯: dozer 來自: 140.109.176.158 (12/15 21:29)
dozer:完成啦^+++^ 12/15 21:30
※ 編輯: dozer 來自: 122.126.35.54 (12/15 23:52)
qrtt1:呵,恭禧啊。分享是件快樂的事啊^^ 12/16 02:28
dozer:補充一點 編譯binary時要記得加-g才能用valgrind來debug 12/19 18:46