說明
使用 VLD 記憶體泄漏檢測工具輔助開發時整理的學習筆記,本篇介紹 VLD 組態檔中配置項 AggregateDuplicates 的使用方法,同系列文章目錄可見 《記憶體泄漏檢測工具》目錄
目錄- 說明
- 1. 組態檔使用說明
- 2. 設定是否顯示重復的泄漏塊
- 2.1 測驗代碼
- 2.2 AggregateDuplicates = no 時的輸出
- 2.3 AggregateDuplicates = yes 時的輸出
- 2.4 輸出結果對比
1. 組態檔使用說明
在程式中通過 #include "vld.h" 的方式檢測記憶體泄漏時,VLD 首先會嘗試在程式的生成目錄下讀取 vld.ini 檔案,若未讀取成功,則會嘗試在 VLD 的安裝目錄下讀取 vld.ini 檔案,若仍未讀取成功,則會使用內置的默認配置,內置的默認配置如果不動原始碼是無法更改的,因此通過修改相應目錄下的 vld.ini 檔案來定制 VLD 功能是最好的選擇,當配置引數等號右邊為空,或者給配置了不合法值時,在使用程序中會被程式重置到默認值,
2. 設定是否顯示重復的泄漏塊
引數名:AggregateDuplicates,
有效賦值:yes,no,
默認值:no,
功能說明:為 yes 時將合并顯示具有相同大小和相同呼叫堆疊的泄漏記憶體塊,只顯示首個泄漏記憶體塊的詳細資訊,并顯示其重復的次數;為 no 時則每次泄漏都會單獨顯示,
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);
for (int i = 1; i < 3; i++)
{
testFun(i);
}
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 AggregateDuplicates = no 時的輸出
標準輸出窗顯示:
ptr = 007f7c98, *ptr = 00000001.
ptr = 007f8058, *ptr = 00000002.
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 0x007F7C98: 4 bytes ----------
Leak Hash: 0xF253A4C4, Count: 1, Total 4 bytes
Call Stack (TID 30620):
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 (16): testVLD.exe!main() + 0x9 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 0x007F8058: 4 bytes ----------
Leak Hash: 0xF253A4C4, Count: 1, Total 4 bytes
Call Stack (TID 30620):
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 (16): testVLD.exe!main() + 0x9 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 AggregateDuplicates = yes 時的輸出
標準輸出窗顯示:
ptr = 00a673d0, *ptr = 00000001.
ptr = 00a67400, *ptr = 00000002.
VLD 輸出報告:
Visual Leak Detector read settings from: D:\Program Files (x86)\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5.1 installed.
Aggregating duplicate leaks.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x00A673D0: 4 bytes ----------
Leak Hash: 0xF253A4C4, Count: 2, Total 8 bytes
Call Stack:
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 (16): testVLD.exe!main() + 0x9 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 2 memory leaks (80 bytes).
Largest number used: 80 bytes.
Total allocations: 80 bytes.
Visual Leak Detector is now exiting.
2.4 輸出結果對比
- 當
AggregateDuplicates = yes時合并顯示了具有相同大小和相同呼叫堆疊的泄漏記憶體塊,且只顯示首個泄漏記憶體塊0x00A673D0的詳細資訊,并顯示其重復的次數及泄漏總大小Count: 2, Total 8 bytes; - 當
AggregateDuplicates = no時每次泄漏都會單獨顯示,因此Count恒為1,仔細觀察可以發現,對于具有相同大小和相同呼叫堆疊的泄漏記憶體塊,它們的Leak Hash值也相同,在測驗例中都為0xF253A4C4,所以不妨推斷Leak Hash的計算由泄漏塊大小及其呼叫堆疊決定,
本文作者:木三百川
本文鏈接:https://www.cnblogs.com/young520/p/17261378.html
著作權宣告:本文系博主原創文章,著作權歸作者所有,商業轉載請聯系作者獲得授權,非商業轉載請附上出處鏈接,遵循 署名-非商業性使用-相同方式共享 4.0 國際版 (CC BY-NC-SA 4.0) 著作權協議,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/548327.html
標籤:其他
上一篇:【深入淺出 Yarn 架構與實作】5-3 Yarn 調度器資源搶占模型
下一篇:Mybatis
