[心得] memory leak test tool: valgrind

看板LinuxDev作者 (rezod)時間18年前 (2006/12/15 15:40), 編輯推噓0(005)
留言5則, 2人參與, 最新討論串1/1
我照著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)

12/15 20:33, , 1F
呵..建議標題-> memory leak test tool: valgrind
12/15 20:33, 1F

12/15 20:33, , 2F
比較方便爬文的人search
12/15 20:33, 2F
※ 編輯: dozer 來自: 140.109.176.158 (12/15 21:29)

12/15 21:30, , 3F
完成啦^+++^
12/15 21:30, 3F
※ 編輯: dozer 來自: 122.126.35.54 (12/15 23:52)

12/16 02:28, , 4F
呵,恭禧啊。分享是件快樂的事啊^^
12/16 02:28, 4F

12/19 18:46, , 5F
補充一點 編譯binary時要記得加-g才能用valgrind來debug
12/19 18:46, 5F
文章代碼(AID): #15Wb5J3L (LinuxDev)
文章代碼(AID): #15Wb5J3L (LinuxDev)