使用 VLD 記憶體泄漏檢測工具輔助開發時整理的學習筆記。本篇介紹 VLD 配置文件中配置項 ReportTo 的使用方法。 ...
說明
使用 VLD 記憶體泄漏檢測工具輔助開發時整理的學習筆記。本篇介紹 VLD 配置文件中配置項 ReportTo 的使用方法。同系列文章目錄可見 《記憶體泄漏檢測工具》目錄
目錄1. 配置文件使用說明
在程式中通過 #include "vld.h"
的方式檢測記憶體泄漏時,VLD 首先會嘗試在程式的生成目錄下讀取 vld.ini
文件,若未讀取成功,則會嘗試在 VLD 的安裝目錄下讀取 vld.ini
文件,若仍未讀取成功,則會使用內置的預設配置,內置的預設配置如果不動源碼是無法更改的,因此通過修改相應目錄下的 vld.ini
文件來定製 VLD 功能是最好的選擇。當配置參數等號右邊為空,或者給配置了不合法值時,在使用過程中會被程式重置到預設值。
2. 設置報告輸出的位置
參數名:ReportTo
。
有效賦值:debugger
,file
,both
。
預設值:debugger
。
功能說明:設置泄漏檢測報告的輸出位置,輸出到調試控制台(debugger
)、輸出到文件(file
)、控制台和文件都輸出(both
),當報告可以輸出到文件時,文件路徑會使用 ReportFile
指定的值(預設值為 .\memory_leak_report.txt
)。
2.1 測試代碼
#include <QCoreApplication>
#include "vld.h"
void testFun(int i)
{
int *ptr = new int(i);
printf("ptr = %08x, *ptr = %08x.\n", ptr, *ptr);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
testFun(1);
return a.exec();
}
測試環境:QT 5.9.2,MSVC 2015 32bit,Debug 模式,VLD 版本為 2.5.1,VLD 配置文件只對該參數做修改,測試工程所在路徑為:E:\Cworkspace\Qt 5.9\QtDemo\testVLD
。
2.2 ReportTo = debugger 時的輸出
標準輸出窗顯示:
ptr = 00f57a30, *ptr = 00000001.
VLD 輸出報告(控制台):
Visual Leak Detector read settings from: D:\Program Files (x86)\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5.1 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x00F57A30: 4 bytes ----------
Leak Hash: 0x78A7E46A, Count: 1, Total 4 bytes
Call Stack (TID 23728):
ucrtbased.dll!malloc()
f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): testVLD.exe!operator new() + 0x9 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (6): testVLD.exe!testFun() + 0x7 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (14): testVLD.exe!main() + 0x7 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): testVLD.exe!__scrt_common_main()
f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): testVLD.exe!mainCRTStartup()
KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0x11E bytes
ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xEE bytes
Data:
01 00 00 00 ........ ........
Visual Leak Detector detected 1 memory leak (40 bytes).
Largest number used: 40 bytes.
Total allocations: 40 bytes.
Visual Leak Detector is now exiting.
2.3 ReportTo = file 時的輸出
標準輸出窗顯示:
ptr = 01059490, *ptr = 00000001.
VLD 輸出報告(控制台):
Visual Leak Detector read settings from: D:\Program Files (x86)\Visual Leak Detector\vld.ini
VLD 輸出報告(文件 memory_leak_report.txt
):
Visual Leak Detector Version 2.5.1 installed.
Outputting the report to E:\Cworkspace\Qt 5.9\QtDemo\build-testVLD-Desktop_Qt_5_9_2_MSVC2015_32bit-Debug\memory_leak_report.txt
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x01059490: 4 bytes ----------
Leak Hash: 0x78A7E46A, Count: 1, Total 4 bytes
Call Stack (TID 15680):
ucrtbased.dll!malloc()
f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): testVLD.exe!operator new() + 0x9 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (6): testVLD.exe!testFun() + 0x7 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (14): testVLD.exe!main() + 0x7 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): testVLD.exe!__scrt_common_main()
f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): testVLD.exe!mainCRTStartup()
KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0x11E bytes
ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xEE bytes
Data:
01 00 00 00 ........ ........
Visual Leak Detector detected 1 memory leak (40 bytes).
Largest number used: 40 bytes.
Total allocations: 40 bytes.
Visual Leak Detector is now exiting.
2.4 ReportTo = both 時的輸出
標準輸出窗顯示:
ptr = 00ebb038, *ptr = 00000001.
VLD 輸出報告(控制台):
Visual Leak Detector read settings from: D:\Program Files (x86)\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5.1 installed.
Outputting the report to the debugger and to E:\Cworkspace\Qt 5.9\QtDemo\build-testVLD-Desktop_Qt_5_9_2_MSVC2015_32bit-Debug\memory_leak_report.txt
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x00EBB038: 4 bytes ----------
Leak Hash: 0x78A7E46A, Count: 1, Total 4 bytes
Call Stack (TID 13352):
ucrtbased.dll!malloc()
f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): testVLD.exe!operator new() + 0x9 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (6): testVLD.exe!testFun() + 0x7 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (14): testVLD.exe!main() + 0x7 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): testVLD.exe!__scrt_common_main()
f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): testVLD.exe!mainCRTStartup()
KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0x11E bytes
ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xEE bytes
Data:
01 00 00 00 ........ ........
Visual Leak Detector detected 1 memory leak (40 bytes).
Largest number used: 40 bytes.
Total allocations: 40 bytes.
Visual Leak Detector is now exiting.
VLD 輸出報告(文件 memory_leak_report.txt
):
Visual Leak Detector Version 2.5.1 installed.
Outputting the report to the debugger and to E:\Cworkspace\Qt 5.9\QtDemo\build-testVLD-Desktop_Qt_5_9_2_MSVC2015_32bit-Debug\memory_leak_report.txt
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x00EBB038: 4 bytes ----------
Leak Hash: 0x78A7E46A, Count: 1, Total 4 bytes
Call Stack (TID 13352):
ucrtbased.dll!malloc()
f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): testVLD.exe!operator new() + 0x9 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (6): testVLD.exe!testFun() + 0x7 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (14): testVLD.exe!main() + 0x7 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): testVLD.exe!__scrt_common_main()
f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): testVLD.exe!mainCRTStartup()
KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0x11E bytes
ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xEE bytes
Data:
01 00 00 00 ........ ........
Visual Leak Detector detected 1 memory leak (40 bytes).
Largest number used: 40 bytes.
Total allocations: 40 bytes.
Visual Leak Detector is now exiting.
2.5 輸出結果對比
- 當
ReportTo = debugger
時,泄漏報告只會輸出到調試控制臺中。 - 當
ReportTo = file
時,泄漏報告只會輸出到指定文件中,控制台只列印配置文件讀取情況。 - 當
ReportTo = both
時,泄漏報告同時輸出到調試控制台與指定文件中,文件中除了第一行沒有輸出配置文件讀取情況外,其他地方與調試控制台的輸出保持一致。
本文作者:木三百川
本文鏈接:https://www.cnblogs.com/young520/p/17263205.html
版權聲明:本文系博主原創文章,著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請附上出處鏈接。遵循 署名-非商業性使用-相同方式共用 4.0 國際版 (CC BY-NC-SA 4.0) 版權協議。