VC 内存泄漏

本文介绍了在VC中使用MS CRT检测内存泄漏的方法,通过示例代码展示了如何使用.DEBUG宏定义来跟踪new、malloc和realloc操作,并展示了内存泄漏检测的输出结果,帮助理解内存泄漏定位。
摘要由CSDN通过智能技术生成

最近在学习VC中MS CRT对内存泄漏的处理。自己总结了一下,写了一小段对new、malloc、realloc检测的代码:

#include "stdafx.h"
#include <Windows.h>
#include <crtdbg.h>

#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK   new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif

#include <crtdbg.h>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#define malloc(x) _malloc_dbg( x, _NORMAL_BLOCK, __FILE__, __LINE__)
#define realloc(x, y) _realloc_dbg( x, y, _NORMAL_BLOCK, __FILE__, __LINE__)
#endif

class A
{
public:
 A() { a1 = new int; a2 = (char*)malloc(12);}
 ~A() { /*delete a1; free(a2);*/ }
 int *a1;
 char *a2;
};

int _tmain(int argc, _TCHAR* argv[])
{
 _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

 int* p = new int;
 //delete p;
 char * y = (char*)malloc(10);
 //free(y)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值