C/C++ 手動記憶體管理易導致洩漏,VLD 是 Visual C++ 專用的**免費、開源**記憶體洩漏檢測庫,提供調用堆疊、洩漏資料內容、報告等級控制,使用極簡單:僅需 `#include
VisualLeakDetector-2.5.1.zip (或更新版)dbghelp.dll` 複製至專案輸出目錄或系統目錄 (舊版 OS 需要)#include #include
#include
#include
void f() {
int *p = new int(0x12345678); // 故意不 delete
printf("p=%08x\n", p);
}
int main() { f(); return 0; }
輸出視窗:
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 57 at 0x003A89C0: 4 bytes ----------
Call Stack:
d:\test\main.cpp(7): f()
d:\test\main.cpp(14): main
f:\rtm\vctools\crt_bld\self_x86\crt\src\crtexe.c(586): __tmainCRTStartup
Data: 78 56 34 12 (0x12345678 以 little-endian 儲存)
Visual Leak Detector detected 1 memory leak.
| 功能 | CRT Debug Heap | VLD |
|---|---|---|
| 洩漏點檔案/行號 | ✓ | ✓ |
| 完整調用堆疊 | ✗ | ✓ |
| 洩漏資料內容 | ✗ | ✓ |
| 報告等級控制 | ✗ | ✓ |
| Release 版影響 | 無 (僅 Debug) | 無 (僅 Debug 連結) |
VLD_REPORT_OPTIONS 控制輸出格式