C/C++ 内存泄露检测 Visual Leak Detector 检测定位内存泄露 对malloc和new均适用

一个用于C/C++内存检测的工具

Visual Leak Detector - Enhanced Memory Leak Detection for Visual C++

下载并解压,将其保存到一个Visual Studio可以方便访问的目录下

(我用的是Visual Studio 2008)

由于不太熟悉VC2008的环境,所以查了一下怎么添加项目的include目录和library目录

reference the library headers in Project Properties->C/C++->General->Additional Include Directories

reference the .lib in Project Properties->Linker->Input

see http://msdn.microsoft.com/en-us/library/ba1z7822.aspx for more details


编写测试程序:

#include "vld.h"
#include <iostream>
using namespace std;

int* getInt() {
    int* p = new int(3);
    return p;
}
 

int main(int argc,char** argv) {
    int* pInt = getInt();
    cout << *pInt << endl;
    //delete pInt;

    return 0;
}

直接看getInt()函数,在main()方法中注释掉"delete pInt;"语句,点击“调试”->“启动调试”,可以调试信息的输出:

Visual Leak Detector Version 1.0 installed (multithreaded DLL).
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 120 at 0x00398F20: 4 bytes ----------
  Call Stack:
    f:\dd\vctools\crt_bld\self_x86\crt\src\dbgmalloc.c (56): malloc
    d:\mutlithread\mutlithread\dead_lock.cpp (6): getInt
    d:\mutlithread\mutlithread\dead_lock.cpp (12): main
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): __tmainCRTStartup
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): mainCRTStartup
    0x7C81776F (File and line number not available): RegisterWaitForInputIdle
  Data:
    03 00 00 00                                                  ........ ........

Visual Leak Detector detected 1 memory leak.
“MutliThread.exe”: 已卸载“C:\WINDOWS\system32\dbghelp.dll”
“MutliThread.exe”: 已卸载“C:\WINDOWS\system32\version.dll”
Visual Leak Detector is now exiting.
调试过程中检测到一处内存泄露,可以双击错误,并定位到错误出处。


拓展阅读:

内存泄露检测工具比较 

内存泄露检测工具



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值