總結信息的詳解:
still reachable: 指的你的指針指向的動態內存還沒有被釋放就退出了,一般來講這種不會出現問題,OS會負責回收
definitely lost: 檢測到內存一定泄漏了,這類錯誤一定要處理。
possibly lost: 說可能有泄漏,一般都是中間有二級指針分配的情況會報錯。
suppressed: 統計了使用valgrind的某些參數取消了的錯誤。
在使用valgrind命令可以引入錯誤消除文件,使得對某些庫產生的錯誤不予提示,但是這些錯誤最終會被統計到suppressed項目中。默認引入的錯誤消除文件在/usr/lib/valgrind/default.supp
Valgrind包括的工具:
Memcheck 內存檢查器
Callgrind 檢查程序中函數調用過程中出現的問題
Cachegrind 檢查程序中緩存使用出現的問題
Helgrind 檢查多線程程序中出現的競爭問題
Massif 檢查程序中堆棧使用中出現的問題
Extension 利用Core提供的功能,自己編寫特定的內存調試工具。
執行:
編譯:gcc –g –O0 sample.c –o sample
加-g爲了獲得更詳細的錯誤信息,-O0防止編譯器優化影響valgrind的判斷。
valgrind --tool=memcheck --leak-check=full ./sample
常用參數:
-q, --quiet
Run silently, and only print error messages. Useful if you are running
regression tests or have some other automated test machinery.
--leak-check=<no|summary|yes|full> [default: summary]
When enabled, search for memory leaks when the client program
finishes. If set to summary, it says how many leaks occurred. If set
to full or yes, it also gives details of each individual leak.
--show-reachable=<yes|no> [default: no]
When disabled, the memory leak detector only shows "definitely lost"
and "possibly lost" blocks. When enabled, the leak detector also shows
"reachable" and "indirectly lost" blocks. (In other words, it shows
all blocks, except suppressed ones, so --show-all would be a better
name for it.)
"reachable" 和 "indirectly" 區別就是,直接是沒有任何指針指向該內存,間接是指指向該內存的指針都位於內存泄露處。