說明
使用 VLD 記憶體泄漏檢測工具輔助開發時整理的學習筆記,本篇介紹 VLD 組態檔中配置項 ReportFile 的使用方法,同系列文章目錄可見 《記憶體泄漏檢測工具》目錄
目錄- 說明
- 1. 組態檔使用說明
- 2. 設定輸出檔案的路徑
- 2.1 測驗代碼
- 2.2 ReportFile 為空時的輸出
- 2.3 ReportFile 指定中文檔案名時的輸出
- 2.3 ReportFile 指定中文路徑時的輸出
- 2.5 輸出結果對比
1. 組態檔使用說明
在程式中通過 #include "vld.h" 的方式檢測記憶體泄漏時,VLD 首先會嘗試在程式的生成目錄下讀取 vld.ini 檔案,若未讀取成功,則會嘗試在 VLD 的安裝目錄下讀取 vld.ini 檔案,若仍未讀取成功,則會使用內置的默認配置,內置的默認配置如果不動原始碼是無法更改的,因此通過修改相應目錄下的 vld.ini 檔案來定制 VLD 功能是最好的選擇,當配置引數等號右邊為空,或者給配置了不合法值時,在使用程序中會被程式重置到默認值,
2. 設定輸出檔案的路徑
引數名:ReportFile,
有效賦值:合法且包含檔案名的檔案路徑,
默認值:.\memory_leak_report.txt,
功能說明:當 ReportFile = file 或 ReportFile = both 時,這個配置項才能起到作用,設定報告輸出的檔案路徑及檔案名,這個路徑可以是絕對路徑,也可以是相對路徑,默認路徑是生成目錄,默認檔案名是 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 組態檔除了對該引數做修改外,還將 ReportFile 設定為 file,測驗工程所在路徑為:E:\Cworkspace\Qt 5.9\QtDemo\testVLD,
2.2 ReportFile 為空時的輸出
標準輸出窗顯示:
ptr = 01027298, *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 0x01027298: 4 bytes ----------
Leak Hash: 0x78A7E46A, Count: 1, Total 4 bytes
Call Stack (TID 27760):
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 ReportFile 指定中文檔案名時的輸出
設定如下:
ReportFile = E:\Cworkspace\Qt 5.9\QtDemo\中文檔案.txt
標準輸出窗顯示:
ptr = 00dea598, *ptr = 00000001.
VLD 輸出報告(控制臺):
Visual Leak Detector read settings from: D:\Program Files (x86)\Visual Leak Detector\vld.ini
在路徑 E:\Cworkspace\Qt 5.9\QtDemo 下生成了以下檔案,中文檔案名亂碼了,

VLD 輸出報告(檔案中),檔案內容沒有亂碼:
Visual Leak Detector Version 2.5.1 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x00DEA598: 4 bytes ----------
Leak Hash: 0x78A7E46A, Count: 1, Total 4 bytes
Call Stack (TID 21832):
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 ReportFile 指定中文路徑時的輸出
設定如下:
ReportFile = E:\Cworkspace\Qt 5.9\QtDemo\中文路徑\test.txt
標準輸出窗顯示:
ptr = 00cfb310, *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 0x00CFB310: 4 bytes ----------
Leak Hash: 0x78A7E46A, Count: 1, Total 4 bytes
Call Stack (TID 10592):
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 里面是不合法的,輸出被重置到了控制臺中,

2.5 輸出結果對比
- 當
ReportFile為空時,將在生成目錄下輸出到檔案memory_leak_report.txt中, - 當
ReportFile指定中文檔案名時,報告能正常輸出到指定檔案,但檔案名會亂碼, - 當
ReportFile指定中文路徑時,報告會被重置輸出到控制臺,不能輸出到指定檔案,
本文作者:木三百川
本文鏈接:https://www.cnblogs.com/young520/p/17263196.html
著作權宣告:本文系博主原創文章,著作權歸作者所有,商業轉載請聯系作者獲得授權,非商業轉載請附上出處鏈接,遵循 署名-非商業性使用-相同方式共享 4.0 國際版 (CC BY-NC-SA 4.0) 著作權協議,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/548465.html
標籤:其他
上一篇:day01-專案介紹和功能實作
