說明
使用 VLD 記憶體泄漏檢測工具輔助開發時整理的學習筆記,本篇介紹 VLD 組態檔中配置項 StartDisabled 的使用方法,同系列文章目錄可見 《記憶體泄漏檢測工具》目錄
目錄- 說明
- 1. 組態檔使用說明
- 2. 設定是否禁用自動初始化
- 2.1 測驗代碼
- 2.2 StartDisabled = no 時的輸出
- 2.3 StartDisabled = yes 時的輸出
- 2.4 輸出結果對比
1. 組態檔使用說明
在程式中通過 #include "vld.h" 的方式檢測記憶體泄漏時,VLD 首先會嘗試在程式的生成目錄下讀取 vld.ini 檔案,若未讀取成功,則會嘗試在 VLD 的安裝目錄下讀取 vld.ini 檔案,若仍未讀取成功,則會使用內置的默認配置,內置的默認配置如果不動原始碼是無法更改的,因此通過修改相應目錄下的 vld.ini 檔案來定制 VLD 功能是最好的選擇,當配置引數等號右邊為空,或者給配置了不合法值時,在使用程序中會被程式重置到默認值,
2. 設定是否禁用自動初始化
引數名:StartDisabled,
有效賦值:yes,no,
默認值:no,
功能說明:設定是否禁用自動初始化,當 StartDisabled = yes 時,將會禁用 VLD 的自動初始化,此時若想檢測程式中的某個函式、某個執行緒是否存在記憶體泄漏(即在運行程序中控制 VLD 的開啟與關閉),需提前人為地呼叫 VLDEnable() 函式來進行初始化,當被測驗的函式結束后,還可以使用 VLDDisable() 函式來關閉 VLD 記憶體檢測,需注意的是,使用這種人為的控制方法時,檢測不到 VLD 關閉期間的記憶體分配引起的記憶體泄漏;此外,呼叫 VLDEnable() 與 VLDDisable() 函式時,僅僅對呼叫函式的執行緒有效,對于同一行程的其他的執行緒無效,除非在每個執行緒中都進行了呼叫,詳見 Controlling-Leak-Detection-at-Runtime,
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);
VLDEnable();
testFun(2);
VLDDisable();
testFun(3);
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 StartDisabled = no 時的輸出
標準輸出窗顯示:
ptr = 00585f68, *ptr = 00000001.
ptr = 00586088, *ptr = 00000002.
ptr = 005860e8, *ptr = 00000003.
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 0x00585F68: 4 bytes ----------
Leak Hash: 0xA089A5DA, Count: 1, Total 4 bytes
Call Stack (TID 33388):
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 ........ ........
---------- Block 2 at 0x00586088: 4 bytes ----------
Leak Hash: 0xF090F245, Count: 1, Total 4 bytes
Call Stack (TID 33388):
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 (17): 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:
02 00 00 00 ........ ........
Visual Leak Detector detected 2 memory leaks (80 bytes).
Largest number used: 80 bytes.
Total allocations: 80 bytes.
Visual Leak Detector is now exiting.
2.3 StartDisabled = yes 時的輸出
標準輸出窗顯示:
ptr = 00797730, *ptr = 00000001.
ptr = 007974f0, *ptr = 00000002.
ptr = 00797550, *ptr = 00000003.
VLD 輸出報告:
Visual Leak Detector read settings from: D:\Program Files (x86)\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5.1 installed.
Starting with memory leak detection disabled.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x007974F0: 4 bytes ----------
Leak Hash: 0xF090F245, Count: 1, Total 4 bytes
Call Stack (TID 7920):
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 (17): 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:
02 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 輸出結果對比
- 當
StartDisabled = no時,能檢測到testFun(1)與testFun(2)引起的記憶體泄漏,由于在呼叫testFun(3)前使用VLDDisable()關閉了 VLD,因此檢測不到testFun(3)引起的記憶體泄漏, - 當
StartDisabled = yes時,只能檢測到testFun(2)引起的記憶體泄漏,因為在呼叫testFun(1)與testFun(3)時 VLD 都處于關閉狀態,
本文作者:木三百川
本文鏈接:https://www.cnblogs.com/young520/p/17263218.html
著作權宣告:本文系博主原創文章,著作權歸作者所有,商業轉載請聯系作者獲得授權,非商業轉載請附上出處鏈接,遵循 署名-非商業性使用-相同方式共享 4.0 國際版 (CC BY-NC-SA 4.0) 著作權協議,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/548548.html
標籤:C++
上一篇:QML和QT
下一篇:Qt原始碼閱讀(三) 物件樹管理
