初識記憶體洩露檢測工具VisualLeakDetector
阿新 • • 發佈:2019-01-29
VLD為vc++下的記憶體洩露檢測工具
1、首先下載安裝vld,直接下載安裝包,安裝過程中會直接新增環境變數。
2、在安裝目錄下有vld.h, vldapi.h, vld.lib, vldmt.lib, vldmtdll.lib, dbghelp.dll等檔案,在使用的時候在附加包含附錄中新增inlude目錄或者將.h檔案拷貝到工程預設的include目錄下
,在附加庫目錄中新增vld安裝目錄下的lib/Win32或者lib/Win64庫目錄或者將相應庫目錄下的lib檔案複製到工程預設的lib庫目錄下即可。
3、在使用的時候,在相應的檔案中包含vld.h標頭檔案,並新增條件編譯語句,則在Debug下,如果有記憶體洩露將會有相應提示。
#ifdef _DEBUG
#include <vld.h>
#endif // DEBUG
#include <iostream>
#include <windows.h>
using namespace std;
int main(int argc, char** argv)
{
char* vldPtr = new char;
char* vldPtr2 = new char[100];
Sleep(1000);
//delete vldPtr;
//delete[]vldPtr2;
return 0;
}
除錯執行,將會出現相應的記憶體洩露資訊,雙擊發生記憶體洩露資訊中的定位資訊,會自動跳到發生記憶體洩露的語句,如下面的:
e:\data\project\myofferinterviewquestions\visualleakdebugtest\vldtest.cpp (13): VisualLeakDebugTest.exe!main() + 0x7 bytes
以及
e:\data\project\myofferinterviewquestions\visualleakdebugtest\vldtest.cpp (14): VisualLeakDebugTest.exe!main() + 0x7 bytes
Visual Leak Detector read settings from: E:\data\C++\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x006F9998: 1 bytes ----------
Leak Hash: 0x877EEED5, Count: 1, Total 1 bytes
Call Stack (TID 3956 ):
MSVCR120D.dll!operator new()
e:\data\project\myofferinterviewquestions\visualleakdebugtest\vldtest.cpp (13): VisualLeakDebugTest.exe!main() + 0x7 bytes
f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c (466): VisualLeakDebugTest.exe!mainCRTStartup()
KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
ntdll.dll!RtlInitializeExceptionChain() + 0x8F bytes
ntdll.dll!RtlInitializeExceptionChain() + 0x5A bytes
Data:
CD ........ ........
---------- Block 2 at 0x00704208: 100 bytes ----------
Leak Hash: 0xE714C3E0, Count: 1, Total 100 bytes
Call Stack (TID 3956):
MSVCR120D.dll!operator new()
f:\dd\vctools\crt\crtw32\stdcpp\newaop.cpp (6): VisualLeakDebugTest.exe!operator new[]() + 0x9 bytes
e:\data\project\myofferinterviewquestions\visualleakdebugtest\vldtest.cpp (14): VisualLeakDebugTest.exe!main() + 0x7 bytes
f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c (466): VisualLeakDebugTest.exe!mainCRTStartup()
KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
ntdll.dll!RtlInitializeExceptionChain() + 0x8F bytes
ntdll.dll!RtlInitializeExceptionChain() + 0x5A bytes
Data:
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD ........ ........
Visual Leak Detector detected 2 memory leaks (173 bytes).
Largest number used: 173 bytes.
Total allocations: 173 bytes.
Visual Leak Detector is now exiting.
程式“[15164] VisualLeakDebugTest.exe”已退出,返回值為 0 (0x0)。
如果沒有發生記憶體洩露,那麼返回;
Visual Leak Detector read settings from: E:\data\C++\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5 installed.
No memory leaks detected.
Visual Leak Detector is now exiting.
程式“[3468] VisualLeakDebugTest.exe”已退出,返回值為 0 (0x0)。