Windows/Linux下C/C++內存泄露檢測工具

 

一 Window下的內存泄露檢測(以VC++環境爲例)

靈活自由是C語言的一大特色,但這個特色也難以避免的帶來一些副作用,比如內存泄露。衆所周知,內存泄露的問題比較複雜,程序正常運行時你看不出它有任何異常,但長時間運行或在特定條件下特定操作重複多次時,它才暴露出來。所以,內存泄露往往是難以發現,也難以定位解決的。

Visual Leak Detector(VLD)是一款用於Visual C++的免費的內存泄露檢測工具,用戶可從http://www.codeproject.com/tools/visualleakdetector.asp下載,該軟件以庫形式與用戶的被測工程一起使用,由於VLD是按LGPL(GNU LESSER GENERAL PUBLIC LICENSE)協議對外開源,所以使用VLD是安全的,不必擔心版權問題。

使用VLD

先從網站下載VLD的zip包,當前最高版本是V1.0,解壓後得到vld.h、vldapi.h、vld.lib、vldmt.lib、vldmtdll.lib、dbghelp.dll等文件,把這些所有.h頭文件拷貝到VC默認的include目錄下,將所有.lib文件拷貝到VC默認的lib目錄下,安裝工作就完成了。

使用VLD很簡單,只須在包含入口函數的CPP或C文件中把vld.h頭文件包含進來即可。該include語句要求放在最前面,如果當前工程定義預編譯head文件(如stdafx.h),則放在“#include <stdafx.h>”語句之後就可以了。之後正常編譯、按Debug方式運行被測程序,等程序運行結束時,查閱VC的output窗口,會有“Visual Leak Detector is now exiting.”一句打印信息,在這條件信息之前,如果當前程序沒有內存泄露會有“No memory leaks detected.”信息打印,但如果有內存泄露,將有類似如下信息打印:

    C:/VcTester21/sample/vc6/SampleMain.c (80): main

    crt0.c (206): mainCRTStartup

    0x7C816FD7 (File and line number not available): RegisterWaitForInputIdle

  Data:

      CD CD CD CD    CD                    ........ ........

   

Visual Leak Detector detected 1 memory leak.

這個信息指明當前發生內存泄露所在的函數及源文件行號,泄露內存塊的地址、長度及當前內存值。用鼠標雙擊指示源碼行的提示信息,VC即自動跳轉到相應代碼行,我們就很方便的知道是哪一行出錯了。

可以看出,VLD用起來很簡單,對它的實現原理感興趣的朋友可以閱讀VLD源碼,也可參考dofty的文章:使用Visual Leak Detector檢測內存泄露

二 Linux下的內存泄露檢測(valgrind)

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

以上內容收集自Internet

 

 

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

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

   按照裏面的README提示,採用標準gnu軟件安裝方式,./configure — make —- makeinstall,安裝後,輸入valgrind ls -l 驗證一下該工具是否工作正常(這是README裏面的方法,實際上是驗證一下對ls -l命令的內存檢測),如果你看到一堆的信息說明你的工具可以使用了。

初次使用

編譯如下代碼: 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 freedint 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)

發佈了67 篇原創文章 · 獲贊 3 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章