:http://www.codeproject.com/KB/applications/visualleakdetector.aspx
如果你在使用 VC 2008 / 2010, 那麼需要下載目前最新的 2.0b , :http://vld.codeplex.com/ 或
http://download.csdn.net/source/3229600
該工具的使用方法很簡單,軟體文檔裡有清晰的說明:
Using Visual Leak Detector
This section briefly describes the basics of using Visual Leak Detector (VLD).
Important! : Before using VLD with any Visual C++ project, you must first add the Visual Leak Detector include and library directories to the Visual C++ include and library directory search paths:
* Visual C++ 8 and 9: Go to Tools -> Options -> Projects and Solutions -> VC++ Directories. Select "Include files" from the "Show Directories For" drop-down menu. Add the include subdirectory from the Visual Leak Detector installation directory. Move it to the bottom of the list. Then select "Library files" from the drop-down menu and add the lib subdirectory from the Visual Leak Detector installation directory. Again, move it to the bottom of the list.
* Visual C++ 7: Go to Project Properties -> C/C++ -> General -> Additional Include Directories and add the include subdirectory from the Visual Leak Detector installation directory. Move it to the bottom of the list. Then select Additional Library Directories and add the lib subdirectory from the Visual Leak Detector installation directory. Again, move it to the bottom of the list.
* Visual C++ 6: Go to Tools -> Options -> Directories. Select "Include files" from the "Show Directories For" drop-down menu. Add the include subdirectory from the Visual Leak Detector installation directory. Move it to the bottom of the list. Then select "Library files" from the drop-down menu and add the lib subdirectory from the Visual Leak Detector installation directory. Again, move it to the bottom of the list.
To use VLD with your project, follow these simple steps:
1. In at least one C/C++ source file from your program, include the vld.h header file. It should not matter which file you add the include statement to. It also should not matter in what order the header is included in relation to other headers. The only exception is stdafx.h (or any other precompiled header). A precompiled header, such as stdafx.h, must always be the first header included in a source file, so vld.h must be included after any precompiled headers.
2. If your program contains one or more DLLs that you would also like to check for memory leaks, then also include vld.h in at least one source file from each DLL to be included in leak detection.
3. Build the debug version of your program.
Note: Unlike earlier (pre-1.9) versions of VLD, it is now acceptable to include vld.h in every source file, or to include it in a common header that is included by many or all source files. Only one copy of the VLD code will be loaded into the process, regardless of how many source files include vld.h.
VLD will detect memory leaks in your program whenever you run the debug version. When you run the program under the Visual C++ debugger, a report of all the memory leaks detected will be displayed in the debugger's output window when your program exits (the report can optionally be saved to a file instead, see ReportFile under Configuration Options). Double-clicking on a source file's line number in the memory leak report will take you to that file and line in the editor window, allowing easy navigation of the code path leading up to the allocation that resulted in the memory leak.
Note: When you build release versions of your program, VLD will not be linked into the executable. So it is safe to leave vld.h included in your source files when doing release builds. Doing so will not result in any performance degradation or any other undesirable overhead.
需要注意的是,由於該軟體是個開源的工具,並不是成熟的商業產品,所以易用性並不是很好,有一些小問題,還是需要我們自己來動手解決的。以下是我遇到的問題和解決方案:
1、F5 運行程式後,提示"無法定位程式輸入焦點 ???? 於動態連結程式庫 dbghelp.dll 上"
解決方案:查看[輸出]視窗,看看載入的是哪個目錄下的 dbghelp.dll 。
如果發現 “???.exe”: 已載入“C:/WINDOWS/system32/dbghelp.dll” 之類的內容
那麼問題出在這裡,程式自動載入了系統目錄下的dbghelp.dll ,
而沒有載入 VLD 的dbghelp.dll。
解決方案:(1)刪除系統目錄下的dbghelp.dll 或暫時改名。(2)或者把 VLD 安裝路徑下的 dbghelp.dll拷貝到你的程式的Debug檔案夾裡。
總之,只要能夠讓程式載入VLD的dbghelp.dll就行了。考慮到一般不要動系統目錄裡的東西,建議採用方法(2)。
採用方法(2)時,不要把 vld_x86.dll 拷貝到Debug目錄下,如果拷了,會使程式堆疊溢位,崩潰。
註:如果vld_x86.dll也不能正常載入,說明沒有設定環境變數,重新安裝VLD,並且在安裝程式詢問是否設定環境變數時,選[Yes]
2、檢測出泄露時,無法顯示泄露的位置,無法定位到CPP檔案和程式碼。並且有大量誤判。
例如:Call Stack:
0x0041907C (File and line number not available): (Function name unavailable)
解決方案:該問題的起因是VLD沒能找到.PDB檔案,導致產生誤判、無法定位代碼位置。要解決該問題,就要從怎麼讓VLD找到.PDB檔案入手
(1)確保VC工程檔案所在的目錄路徑不含中文字元、雙位元組字元。(VLD對中文路徑支援不了)
(2)查看是否正常產生了.pdb檔案。