实操VS2015配置内存泄漏检查工具Visual Leak Detector

来源:互联网 发布:什么叫分布式系统 知乎 编辑:程序博客网 时间:2024/06/10 23:20

VS配置及使用Visual Leak Detector(检测内存泄漏工具)

 下载VLD,可以去官网下载.

http://vld.codeplex.com/


默认安装后记录安装目录(以下简称VLDpath),例如C:\Program Files (x86)\Visual Leak Detector

 

3.配置Visual Leak Detetctor的输出文件.

  >VLDpath中打开vld.ini文件,ReportTo设置为both.

 

 

4.VS项目添加该工具的配置

 

 ①打开项目属性页                                                                  ②分别在VC++目录中包含目录库目录中添加路径  VLDpath\includeVLDpath\lib

                               

            

 

5.使用方法简介

  需要检测内存泄漏的函数的.cpp文件中包含vld.h即可.

    即加入#include<vld.h>

  注意:如果这个cpp文件中包含了stdafx.h,则将包含vld.h的语句放在stdafx.h的包含语句之后,否则放在最前面

 

 

6.测试一下

 >新建一个win32控制台应用程序项目,这里我们将#include<vld.h>”stdafx.cpp”的如下位置.

 // stdafx.cpp : 只包括标准包含文件的源文件
// ConsoleApplication2.pch 将作为预编译头
// stdafx.obj 将包含预编译类型信息


#include<vld.h> //// 包含VLD的头文件
#include "stdafx.h"

 

>示例程序

#include "stdafx.h"

#include<stdlib.h>

void f()

{

int *p_int = new int(12345);

char *p_char = new char('k');

printf("int=%d,char=%c ", *p_int, *p_char);

//free(p_int);

//free(p_char);

}

int main()

{

f();

    return 0;

}

 

 

 

 

如何查看检测结果?

   使用情况1.

    Ctrl+F5直接运行程序,可以看到程序框中出现了Visual Leak Detector字样,表明我们的配置已经OK


    接着会发现在工程目录下面生成一个文件“memory_leak_report.txt”

  可以从中找到问题代码的位置行数.

Visual Leak Detector Version 2.5.1 installed.
    Outputting the report to the debugger and to C:\Users\bm00076\Documents\Visual Studio 2015\Projects\ConsoleApplication2\ConsoleApplication2\memory_leak_report.txt
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x005EC3C0: 4 bytes ----------
  Leak Hash: 0x170D4305, Count: 1, Total 4 bytes
  Call Stack (TID 5932):
    ucrtbased.dll!malloc()
    f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): ConsoleApplication2.exe!operator new() + 0x9 bytes
    c:\users\bm00076\documents\visual studio 2015\projects\consoleapplication2\consoleapplication2\consoleapplication2.cpp (9):ConsoleApplication2.exe!f() + 0x7 bytes
    c:\users\bm00076\documents\visual studio 2015\projects\consoleapplication2\consoleapplication2\consoleapplication2.cpp (27): ConsoleApplication2.exe!main()
    f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): ConsoleApplication2.exe!invoke_main() + 0x1B bytes
    f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): ConsoleApplication2.exe!__scrt_common_main_seh() + 0x5 bytes
    f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): ConsoleApplication2.exe!__scrt_common_main()
    f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): ConsoleApplication2.exe!mainCRTStartup()
    kernel32.dll!BaseThreadInitThunk() + 0x12 bytes
    ntdll.dll!RtlInitializeExceptionChain() + 0x63 bytes
    ntdll.dll!RtlInitializeExceptionChain() + 0x36 bytes
  Data:
    39 30 00 00                                                  90...... ........ 

 

    使用情况2.

按下F5调试命令,程序运行完毕之后,我们在输出栏中,发现以下类似于“memory_leak_report.txt”内容

 

直接点击,便跳转到问题代码的位置行了.

 

7.其他问题

  如在安装或者使用过程中遇到其他疑问,欢迎讨论!

 

阅读全文
0 1
原创粉丝点击