說明
使用 VLD 記憶體泄漏檢測工具輔助開發時整理的學習筆記,
目錄- 說明
- 1. 使用方式
- 2. 有三處記憶體泄漏時的輸出報告
- 3. 有兩處記憶體泄漏時的輸出報告
1. 使用方式
在 QT 中使用 VLD 的方法可以查看另外幾篇博客:
-
【Visual Leak Detector】在 QT 中使用 VLD(方式一)
-
【Visual Leak Detector】在 QT 中使用 VLD(方式二)
-
【Visual Leak Detector】在 QT 中使用 VLD(方式三)
本次測驗使用的環境為:QT 5.9.2,MSVC 2015 32bit,Debug 模式,VLD 版本為 2.5.1,VLD 組態檔不做任何更改使用默認配置,測驗工程所在路徑為:E:\Cworkspace\Qt 5.9\QtDemo\testVLD,
2. 有三處記憶體泄漏時的輸出報告
寫一個有三處記憶體泄漏的程式,如下:
#include <QCoreApplication>
#include "vld.h"
void testFun1()
{
int *ptr = new int(0x55345678);
printf("ptr1 = %08x, *ptr1 = %08x.\n", ptr, *ptr);
}
void testFun2()
{
short *ptr = new short(0x4529);
printf("ptr2 = %08x, *ptr2 = %04x.\n", ptr, *ptr);
}
void testFun3()
{
char *ptr = new char[3];
printf("ptr3 = %08x.\n", ptr, *ptr);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
testFun1();
testFun2();
testFun3();
return a.exec();
}
程式運行時,在標準輸出窗會輸出以下結果:
ptr1 = 00b674b8, *ptr1 = 55345678.
ptr2 = 00b670f8, *ptr2 = 4529.
ptr3 = 00b674e8.
程式運行結束后,檢測到了記憶體泄漏,VLD 會輸出以下報告(本例中出現三處記憶體泄漏),第 1~3 行顯示 VLD 運行狀態,第 4~19 行顯示 testFun2() 函式泄漏記憶體的詳細資訊,第 22~37 行顯示 testFun1() 函式泄漏記憶體的詳細資訊,第 40~55 行顯示 testFun3() 函式泄漏記憶體的詳細資訊,第 58~60 行總結此次泄漏情況,第 61 行顯示 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 2 at 0x00B670F8: 2 bytes ----------
Leak Hash: 0xB9FC7D06, Count: 1, Total 2 bytes
Call Stack (TID 14904):
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 (12): testVLD.exe!testFun2() + 0x7 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (28): testVLD.exe!main()
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:
29 45 )E...... ........
---------- Block 1 at 0x00B674B8: 4 bytes ----------
Leak Hash: 0xE622CBED, Count: 1, Total 4 bytes
Call Stack (TID 14904):
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!testFun1() + 0x7 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (27): testVLD.exe!main()
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:
78 56 34 55 xV4U.... ........
---------- Block 3 at 0x00B674E8: 3 bytes ----------
Leak Hash: 0x1ED7DC7D, Count: 1, Total 3 bytes
Call Stack (TID 14904):
ucrtbased.dll!malloc()
f:\dd\vctools\crt\vcstartup\src\heap\new_array.cpp (15): testVLD.exe!operator new[]() + 0x9 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (18): testVLD.exe!testFun3() + 0x7 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (30): testVLD.exe!main()
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:
CD CD CD ........ ........
Visual Leak Detector detected 3 memory leaks (117 bytes).
Largest number used: 117 bytes.
Total allocations: 117 bytes.
Visual Leak Detector is now exiting.
觀察以上輸出報告,可以總結出以下規律:
Block后面的序號和函式呼叫順序有關,先呼叫的序號小,在主函式中testFun1()、testFun2()、testFun3()是依次呼叫的,分別對應于Block 1、Block 2、Block 3,- 報告輸出時泄漏資訊的顯示順序和地址大小有關,地址小的先顯示,
Block 1、Block 2、Block 3的泄漏首地址分別為0x00B674B8、0x00B670F8、0x00B674E8,記憶體地址從小到大排序為Block 2、Block 1、Block 3,顯示順序正是如此,
每個 Block 輸出的詳細決議可以查看另外一篇博客 【Visual Leak Detector】QT 中 VLD 輸出決議(二),第 58~60 行中的 117 bytes 包含有: Block 1 中申請 int 的 4 bytes 及對應的 36 bytes 記憶體管理頭、 Block 2 中申請 short 的 2 bytes 及對應的 36 bytes 記憶體管理頭、 Block 3 中申請 char[3] 的 3 bytes 及對應的 36 bytes 記憶體管理頭,共計 \(4 + 36 + 2 + 36 + 3 + 36 = 117 bytes\),
3. 有兩處記憶體泄漏時的輸出報告
將 testFun2() 函式中申請的記憶體正常釋放,測驗代碼如下:
#include <QCoreApplication>
#include "vld.h"
void testFun1()
{
int *ptr = new int(0x55345678);
printf("ptr1 = %08x, *ptr1 = %08x.\n", ptr, *ptr);
}
void testFun2()
{
short *ptr = new short(0x4529);
printf("ptr2 = %08x, *ptr2 = %04x.\n", ptr, *ptr);
delete ptr;
}
void testFun3()
{
char *ptr = new char[3];
printf("ptr3 = %08x.\n", ptr, *ptr);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
testFun1();
testFun2();
testFun3();
return a.exec();
}
程式運行時,在標準輸出窗會輸出以下結果:
ptr1 = 006c5c68, *ptr1 = 55345678.
ptr2 = 006c5cc8, *ptr2 = 4529.
ptr3 = 006c5db8.
程式運行結束后,檢測到了記憶體泄漏,VLD 會輸出以下報告(本例中出現兩處記憶體泄漏),第 1~3 行顯示 VLD 運行狀態,第 4~19 行顯示 testFun1() 函式泄漏記憶體的詳細資訊,第 22~37 行顯示 testFun3() 函式泄漏記憶體的詳細資訊,第 40~42 行總結此次泄漏情況,第 43 行顯示 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 0x006C5C68: 4 bytes ----------
Leak Hash: 0x1E4EE072, Count: 1, Total 4 bytes
Call Stack (TID 11992):
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!testFun1() + 0x7 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (28): testVLD.exe!main()
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:
78 56 34 55 xV4U.... ........
---------- Block 3 at 0x006C5DB8: 3 bytes ----------
Leak Hash: 0xE91F4A96, Count: 1, Total 3 bytes
Call Stack (TID 11992):
ucrtbased.dll!malloc()
f:\dd\vctools\crt\vcstartup\src\heap\new_array.cpp (15): testVLD.exe!operator new[]() + 0x9 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (19): testVLD.exe!testFun3() + 0x7 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (31): testVLD.exe!main()
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:
CD CD CD ........ ........
Visual Leak Detector detected 2 memory leaks (79 bytes).
Largest number used: 79 bytes.
Total allocations: 117 bytes.
Visual Leak Detector is now exiting.
觀察以上輸出報告,可知 Block 后面的序號不僅僅針對導致記憶體泄漏的函式,這個例子中的 Block 2 應該屬于 testFun2(),但由于 testFun2() 正常釋放了記憶體,因此在輸出報告中沒有找到 Block 2,只能找到 Block 1 和 Block 3,這個例子說明了輸出報告中 Block 序號不一定是連續的,可能缺失了一些序號,屬正常現象,第 40~41 行中的 79 bytes 包含有: Block 1 中申請 int 的 4 bytes 及對應的 36 bytes 記憶體管理頭、 Block 3 中申請 char[3] 的 3 bytes 及對應的 36 bytes 記憶體管理頭,共計 \(4 + 36 + 3 + 36 = 79 bytes\),第 42 行中的 117 bytes 表示運行程序中一共分配的記憶體大小,計算方式與上一節一樣,
本文作者:木三百川
本文鏈接:https://www.cnblogs.com/young520/p/17259898.html
著作權宣告:本文系博主原創文章,著作權歸作者所有,商業轉載請聯系作者獲得授權,非商業轉載請附上出處鏈接,遵循 署名-非商業性使用-相同方式共享 4.0 國際版 (CC BY-NC-SA 4.0) 著作權協議,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/548211.html
標籤:其他
