使用Visual Leak Detector for Visual C++ 捕捉内存泄露---Visual C++内存泄露检测—VLD工具使用说明

from:http://www.cnblogs.com/chuncn/archive/2012/12/05/2803450.html

以下经过测试:

使用Visual Leak Detector for Visual C++ 捕捉内存泄露
1、下载vlc

http://vld.codeplex.com/

2、创建应用并配置

c/c++ -> General -> Additional Include Directories = C:\Program Files (x86)\Visual Leak Detector\include

Linker -> General -> Additional Library Directories = C:\Program Files (x86)\Visual Leak Detector\lib\Win32

3、捕捉内在泄露

复制代码
#include "stdafx.h"

#ifdef _DEBUG  
#include "vld.h"
#endif

int _tmain(int argc, _TCHAR* argv[])
{
    char* c1 = new char[100];
    char* c2  =(char*)malloc(200);
    char c3[1000];

    //delete[] c1;
    //free(c2);

    return 0;
}

错误:#error : [VLD COMPILE ERROR] '#include <vld.h>' should appear before '#include <afxwin.h>' in file stdafx.h

解决办法:

在stdafx.h有#include <afxwin.h>

将#include <vld.h>放在#include <afxwin.h>之前,可以解决问题
复制代码
输入如下:

复制代码
Visual Leak Detector Version 2.2.3 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x008CD6F8: 100 bytes ----------
  Call Stack:
    c:\users\enost_000\desktop\consoleapplication1\consoleapplication1\consoleapplication1.cpp (12): ConsoleApplication1.exe!wmain + 0x7 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (533): ConsoleApplication1.exe!__tmainCRTStartup + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): ConsoleApplication1.exe!wmainCRTStartup
    0x74EE8543 (File and line number not available): KERNEL32.DLL!BaseThreadInitThunk + 0xE bytes
    0x770EAC69 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x85 bytes
    0x770EAC3C (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x58 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                                                  ........ ........


---------- Block 2 at 0x008CD798: 200 bytes ----------
  Call Stack:
    c:\users\enost_000\desktop\consoleapplication1\consoleapplication1\consoleapplication1.cpp (13): ConsoleApplication1.exe!wmain + 0xD bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (533): ConsoleApplication1.exe!__tmainCRTStartup + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): ConsoleApplication1.exe!wmainCRTStartup
    0x74EE8543 (File and line number not available): KERNEL32.DLL!BaseThreadInitThunk + 0xE bytes
    0x770EAC69 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x85 bytes
    0x770EAC3C (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x58 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 2 memory leaks (372 bytes).
Largest number used: 372 bytes.
Total allocations: 372 bytes.
Visual Leak Detector is now exiting.
The program '[0x262C] ConsoleApplication1.exe' has exited with code 0 (0x0).
复制代码
 

4、qt也vlc
  vlc只支持vc编译器,所以 qt的话只能够使用vc编译器捕捉并解决内存泄露之后,再考虑使用mingw(gcc/g++)编译程序。

 

以下可以做参考,本人未测试过:

from:

http://www.cnblogs.com/lidabo/archive/2012/07/19/2599745.html

Visual C++内存泄露检测VLD工具使用说明

一.        VLD工具概述

Visual Leak DetectorVLD)是一款用于Visual C++的免费的内存泄露检测工具。他的特点有:可以得到内存泄漏点的调用堆栈,如果可以的话,还可以得到其所在文件及行号; 可以得到泄露内存的完整数据; 可以设置内存泄露报告的级别;并且是开源免费的。

 

二.        VLD下载

http://www.codeproject.com/tools/visualleakdetector.asp

本文后附有vld1.0的工具包,下载解包后就可使用。

 

三.        VLD安装

方法一:

解压之后得到vld.h, vldapi.h, vld.lib, vldmt.lib, vldmtdll.lib, dbghelp.dll等文件。将.h文件拷贝到Visual C++的默认include目录下,将.lib文件拷贝到Visual C++的默认lib目录下,将dbghelp.dll拷贝到你的程序的运行目录下,便安装完成了

方法二:

解压之后得到vld.h, vldapi.h, vld.lib, vldmt.lib, vldmtdll.lib, dbghelp.dll等文件。将.h文件和.lib文件拷贝到你要检测的工程文件所在的目录里(只针对此工程),将dbghelp.dll拷贝到你的程序的运行目录下。就完成安装了。

 

四.        VLD使用

在包含入口函数的.cpp文件中包含vld.h就可以了。下面以一个例子进行说明(源程序见附录)

1. 加入头文件:

2.      编译:

3.      debug方式下运行:查看VC的输出信息:

内存泄露所在的位置

 

 


4查看VC输出信息:

 "WARNING: Visual Leak Detector detected meory leaks!"


 

5. 如果没有内存泄露,此输出的信息为:

      "No memory leaks detected"

 

 

五.        附录

1.测试用文件

#include "vld.h"

 

#include "iostream.h"

#include "stdio.h"

#include "stdlib.h"

#include "string.h"

 

void Function1(char *p)

{

       char *pTmp = new char[255];

       memset(pTmp, 0x0, 255);

       strncpy(pTmpp, 255);

       //delete pTmp;

}

 

int Function2(void)

{

       char acString[] = "this is test!";

       Function1(acString);

       return 1;

}

 

void Function3(void)

{

       Function2();

}

int main(void)

{

       cout << "begin.............." << endl;

 

       Function3();

       cout << "end................" << endl;

       return 1;

}

 

2Vld1.0工具包

 

 

3.关于linux的内存泄露,可以使用valgrind工具进行检测。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值