調試內存

 linux使用valgrind 工具檢查內存泄露

Valgrind是一款非常強大的工具集合,它包含有包括內存檢測、CPU監測等多種工具,其中最常用的是內存檢測功能,它能監測出以下的各種內存錯誤:
1. 訪問非法內存區域
2. 使用未被初始化的內存區域
3. 非法釋放內存,比如多次free一個內存
4. 內存泄露

使用步驟:
1. 使用valgrind前需要使用-g參數編譯源程序以便生成debug信息
2. 在運行程序的命令行前加上valgrind,例如:valgrind myprog argvlist
   爲了能夠給出內存檢測報告,需要加上--leak-check參數,那麼上述命令就變成:valgrind --leak-check=yes myprog argvlist
   這個時候程序運行會非常慢,消耗資源也會大幅增加,這是正常的,不必擔心。因爲valgrind需要收集內存錯誤和泄露的詳細信息。這被有些人認爲是valgrind的缺點,但是對於一個大項目來說,這一點時間的消耗來監測程序的穩定性是值得的。
3. 閱讀valgrind給出的報告
valgrind的其它參數:
--num-callers=N:指定報告中調用棧的層數,這在定位和跟蹤錯誤的時候會比較有用
-v :可以打印出更加詳細的信息
valgrind的報告:
"Invalid write/read...":這一般是訪問非法的內存區域,比如數組越界等
"Conditional jump or move depends on uninitialised value(s)":使用了未初始化的變量(包括未初始化的局部變量和未初始化的堆區域)
"Mismatched free() / delete / delete []":這一般是非法釋法內存的錯誤,比如多次free一個內存
"LEAK SUMMARY":這表示下面是內存泄露的信息(造成內存丟失的程序行可以通過查看這一行前面的清單來定位)
"definitely lost":肯定丟失的部分,這種報告必須處理
"possibly lost":可能丟失的部分,這是由於C/C++語言指針處理的特點造成的,這部分可能不太準確
 
 
這裏只有一個簡單的介紹,更多詳細信息參見valgrind的官方網站:http://valgrind.org/docs/manual/



Valgrind 是在linux系統下開發應用程序時用於調試內存問題的工具。它尤其擅長髮現內存管理的問題,它可以檢查程序運行時的內存泄漏問題。

   它的官方網址是 http://www.valgrind.org/

   下載最新版本的Valgrind,目前是3.2.0。 wget http://www.valgrind.org/downloads/valkyrie-1.2.0.tar.bz2

   執行常規的安裝步驟:./confgure && make && make install。注意: 系統必須安裝QT的開發包。即便這樣在make 時還是出現qplatformdefs.h這個文件找不到的情況,導致make失敗。查找系統中的qplatformdefs.h 之後,發現沒有存在於qt的標準頭文件目錄/usr/lib/qt-3.3/include。如是將/usr/lib/qt- 3.3/mkspecs/linux-g++/ 目錄下該頭文件複製標準頭文件目錄,重新make ,後面一切OK。

初次使用
    編譯如下代碼:  gcc -Wall example.c -g -o example 

#include <stdlib.h>

void f(void)
{
int* x = malloc(10 * sizeof(int));
x[10] = 0; // problem 1: heap block overrun
} // problem 2: memory leak -- x not freed

int main(void)
{
f();
return 0;
}

     注意:gcc 的-g 選項讓Valgrind調試輸出時指出相應信息的代碼所在的行號。

 

valgrind --tool=memcheck --leak-check=yes ./example

==6742== Memcheck, a memory error detector for x86-linux.
==6742== Copyright (C) 2002-2004, and GNU GPL'd, by Julian Seward et al.
==6742== Using valgrind-2.2.0, a program supervision framework for x86-linux.
==6742== Copyright (C) 2000-2004, and GNU GPL'd, by Julian Seward et al.
==6742== For more details, rerun with: -v
==6742==
==6742== Invalid write of size 4
==6742==    at 0x8048384: f (example.c:6)
==6742==    by 0x80483AC: main (example.c:12)
==6742==  Address 0x1B908050 is 0 bytes after a block of size 40 alloc'd
==6742==    at 0x1B904984: malloc (vg_replace_malloc.c:131)
==6742==    by 0x8048377: f (example.c:5)
==6742==    by 0x80483AC: main (example.c:12)
==6742==
==6742== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 12 from 1)
==6742== malloc/free: in use at exit: 40 bytes in 1 blocks.
==6742== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.
==6742== For counts of detected errors, rerun with: -v
==6742== searching for pointers to 1 not-freed blocks.
==6742== checked 1360800 bytes.
==6742==
==6742==
==6742== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==6742==    at 0x1B904984: malloc (vg_replace_malloc.c:131)
==6742==    by 0x8048377: f (example.c:5)
==6742==    by 0x80483AC: main (example.c:12)
==6742==
==6742== LEAK SUMMARY:
==6742==    definitely lost: 40 bytes in 1 blocks.
==6742==    possibly lost:   0 bytes in 0 blocks.
==6742==    still reachable: 0 bytes in 0 blocks.
==6742==         suppressed: 0 bytes in 0 blocks.
==6742== Reachable blocks (those to which a pointer was found) are not shown.
==6742== To see them, rerun with: --show-reachable=yes

   上面的C程序存在兩個錯誤:1. 數組下標越界;2. 分配的內存沒有釋放,存在內存泄露的問題。對於錯誤1,看Valgrind的調試信息片斷

==6742== Invalid write of size 4
==6742==    at 0x8048384: f (example.c:6)
==6742==    by 0x80483AC: main (example.c:12)
==6742==  Address 0x1B908050 is 0 bytes after a block of size 40 alloc'd
==6742==    at 0x1B904984: malloc (vg_replace_malloc.c:131)
==6742==    by 0x8048377: f (example.c:5)

對於錯誤2,看這個

==6742== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.

......

==6742== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==6742==    at 0x1B904984: malloc (vg_replace_malloc.c:131)
==6742==    by 0x8048377: f (example.c:5)
==6742==    by 0x80483AC: main (example.c:12)

 

 

相關鏈接:

   http://www.valgrind.org/docs/manual/quick-start.html

   http://www-128.ibm.com/developerworks/cn/linux/l-pow-debug/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章