說明
使用 VLD 記憶體泄漏檢測工具輔助開發時整理的學習筆記,本篇介紹如何在 Release 模式下使用 VLD,同系列文章目錄可見 《記憶體泄漏檢測工具》目錄
目錄- 說明
- 1. 思路概述
- 2. 在 QT 中實踐
1. 思路概述
要在 RELEASE 模式下使用 VLD,必須在包含頭檔案 vld.h 前預先定義 VLD_FORCE_ENABLE 宏(參考 VLD Issues 46):
#define VLD_FORCE_ENABLE
#include "vld.h"
與 DEBUG 模式一樣,可以在代碼中使用 VLDGlobalEnable、VLDReportLeaks、VLDGlobalDisable 等 VLD 庫提供的 API,也可以通過提前更改 vld.ini 組態檔,實作對 VLD 功能的定制,API 使用方法可查看 vld.h 頭檔案或本人同系列文章,
2. 在 QT 中實踐
本次測驗使用的環境為:QT 5.9.2,MSVC 2015 32bit,Release 模式,VLD 版本為 2.5.1,VLD 組態檔不做任何更改使用默認配置,測驗工程所在路徑為:E:\Cworkspace\Qt 5.9\QtDemo\testVLD,測驗代碼如下:
#include <QCoreApplication>
#define VLD_FORCE_ENABLE
#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();
}
運行結束后,標準輸出窗中輸出以下結果:
ptr = 00e48270, *ptr = 00000001.
程式運行結束后,輸出以下報告:
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 0x00E48270: 4 bytes ----------
Leak Hash: 0xBE4F898C, Count: 1, Total 4 bytes
Call Stack (TID 37180):
ucrtbase.dll!malloc()
testVLD.exe!0x00A510FA()
testVLD.exe!0x00A51059()
testVLD.exe!0x00A512C6()
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 (4 bytes).
Largest number used: 4 bytes.
Total allocations: 4 bytes.
Visual Leak Detector is now exiting.
為了與 DEBUG 模式下的輸出做比較,切換為 DEBUG 模式后的輸出報告如下:
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 0x01123E48: 4 bytes ----------
Leak Hash: 0x57574D54, Count: 1, Total 4 bytes
Call Stack (TID 28696):
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 (8): testVLD.exe!testFun() + 0x7 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (16): 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.
比較可知,RELEASE 模式下的報告,呼叫堆疊資訊不夠詳細,且泄漏總記憶體中沒有用于記憶體管理頭的額外 36 bytes,
本文作者:木三百川
本文鏈接:https://www.cnblogs.com/young520/p/17311024.html
著作權宣告:本文系博主原創文章,著作權歸作者所有,商業轉載請聯系作者獲得授權,非商業轉載請附上出處鏈接,遵循 署名-非商業性使用-相同方式共享 4.0 國際版 (CC BY-NC-SA 4.0) 著作權協議,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/549846.html
標籤:其他
下一篇:java -- 二維陣列
