VLD工具可以用来检查VS C++程序的内存泄露。
VLD官网:https://kinddragon.github.io/vld/
官网不方便下载的,可以用我的链接:https://pan.baidu.com/s/1-SiP9bYCfk67aUmKIOUwpQ 提取码:dvaw
下载安装VLD后会有 include, lib, bin,vlc.ini等文件。把include, lib, bin添加到程序中,如果是C++老鸟,应该不难。然后在代码中添加头文件 #include”vld.h” 即可, 例如下面的代码
#include
#include "vld.h"
#pragma comment(lib, "vld.lib")
int main()
{
int *arr = new int[10];
return 0;
}
数组arr没有被delete, 进程结束后,仍然有40个字节的内存片段泄露。把VLD bin目录除了dbghelp.dll的文件复制到程序可执行目录,去你的VS2017安装路径找到dbghelp.dll,例如我的路径:C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\TestPlatform\Extensions\Cpp\x64,把该路径的dbghelp.dll复制到程序可执行目录。
运行后,经过VLD检测,在VS的输出
直接定位出哪一行除了内存泄漏。该内容也可以写到文件中,在做压力测试时,可以比较直观的查看。把安装目录vld.ini复制到程序exe所在目录,查看ReportFile和ReportTo的配置,这英语不难,可以自行设置,如下:
; Sets the report file destination, if reporting to file is enabled. A relative
; path may be specified and is considered relative to the process’ working
; directory.
;
; Valid Values: Any valid path and filename.
; Default: .\memory_leak_report.txt
;
ReportFile = .\memory_leak123.txt
; Sets the report destination to either a file, the debugger, or both. If
; reporting to file is enabled, the report is sent to the file specified by the
; ReportFile option.
;
; Valid Values: debugger, file, both
; Default: debugger
;
ReportTo = file
测试时可以设为绝对路径,我这里设置的是相对路径,会跑到代码目录,如下图:
Original: https://www.cnblogs.com/carl2380/p/15905105.html
Author: 张纯睿
Title: !! vc C++ VS2017检查内存泄漏具体到某一行代码
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/535026/
转载文章受原作者版权保护。转载请注明原作者出处!