参考:
Visual C++ 内存泄露检测工具(VLD)_vs内存泄漏检测工具-CSDN博客
内存泄露检测工具VLD(Visual Leak Detector)使用说明-CSDN博客
1.安装VLD
因为github上的资源下载不下来,就在csdn上找了一个相关资源。
2.需要思考为什么没有具体到哪个文件的哪一行有内存泄漏的相关信息?
我的环境是VS2019。
尝试了很多方法。
Visual Leak Detector(vld)无法显示内存泄露行号_vld不显示行数-CSDN博客
最后解决我的问题的是一句话:
程序正常允许并且不报错,但只显示泄漏内存的大小,不显示行号,检查VS项目路径中是否有中文。vld只支持英文路径!
改为英文路径后,终于显示出了行号!!!
3.只有Debug模式下,vld才会工作,显示信息!
4.示例(1):
#include <iostream>
#include "vld.h"
int main(int argc, char* argv[])
{
char* pBuf = new char[200];
return 0;
}
Visual Leak Detector read settings from: E:\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 0x00D2BE08: 200 bytes ----------
Leak Hash: 0x45765F71, Count: 1, Total 200 bytes
Call Stack (TID 16920):
ucrtbased.dll!malloc()
D:\a\_work\1\s\src\vctools\crt\vcstartup\src\heap\new_array.cpp (28): Project1.exe!operator new[]() + 0x9 bytes
D:\Qt\Qt5Book\work\vld\Project1\Project1\main.cpp (6): Project1.exe!main() + 0xA bytes
D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl (78): Project1.exe!invoke_main() + 0x2D bytes
D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl (288): Project1.exe!__scrt_common_main_seh() + 0x5 bytes
D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl (331): Project1.exe!__scrt_common_main()
D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): Project1.exe!mainCRTStartup()
KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
ntdll.dll!RtlGetFullPathName_UEx() + 0x4BF bytes
ntdll.dll!RtlGetFullPathName_UEx() + 0x48D bytes
Data:
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD ........ ........
CD CD CD CD CD CD CD CD ........ ........
Visual Leak Detector detected 1 memory leak (236 bytes).
Largest number used: 236 bytes.
Total allocations: 236 bytes.
Visual Leak Detector is now exiting.
5.示例(2):
#include "QtWidgetsApplication1.h"
#include <QtWidgets/QApplication>
#include "vld.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QtWidgetsApplication1 w;
w.show();
char* m = new char;
return a.exec();
}
Visual Leak Detector read settings from: E:\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 0x01405818: 1 bytes ----------
Leak Hash: 0x489DD070, Count: 1, Total 1 bytes
Call Stack (TID 23652):
ucrtbased.dll!malloc()
D:\a\_work\1\s\src\vctools\crt\vcstartup\src\heap\new_scalar.cpp (35): QtWidgetsApplication1.exe!operator new() + 0x9 bytes
D:\Qt\vld\QtWidgetsApplication1\QtWidgetsApplication1\main.cpp (9): QtWidgetsApplication1.exe!main() + 0x7 bytes
D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl (78): QtWidgetsApplication1.exe!invoke_main() + 0x2D bytes
D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl (288): QtWidgetsApplication1.exe!__scrt_common_main_seh() + 0x5 bytes
D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl (331): QtWidgetsApplication1.exe!__scrt_common_main()
D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): QtWidgetsApplication1.exe!mainCRTStartup()
KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
ntdll.dll!RtlGetFullPathName_UEx() + 0x4BF bytes
ntdll.dll!RtlGetFullPathName_UEx() + 0x48D bytes
Data:
CD ........ ........
Visual Leak Detector detected 1 memory leak (37 bytes).
Largest number used: 37 bytes.
Total allocations: 37 bytes.
Visual Leak Detector is now exiting.
需要注意的是,对于该段程序,检测结果在关闭窗口后才显示出来。
具体来讲,应该是在消息循环结束之后,检测结果才会打印。(实验了两次)
6.对于本机的环境来讲,后面的Visual studio相关的项目添入
#include "vld.h"
就可以直接进行内存泄漏的判断!