VC 内存泄漏

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在学习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);
 char * x = (char*)malloc(5);
 x[0] = 'm';
 //free(x);
 realloc( x, 100);
    A a;
    A * ap = new A();

 return 0;
}

 

输出结果如下:

Detected memory leaks!
Dumping objects ->
g:/windows核心编程代码/testmemoryleak/testmemoryleak1/testmemoryleak1.cpp(25) : {100} normal block at 0x00394BB8, 12 bytes long.
 Data: <            > CD CD CD CD CD CD CD CD CD CD CD CD
g:/windows核心编程代码/testmemoryleak/testmemoryleak1/testmemoryleak1.cpp(25) : {99} client block at 0x00394B78, subtype 0, 4 bytes long.
 Data: <    > CD CD CD CD
g:/windows核心编程代码/testmemoryleak/testmemoryleak1/testmemoryleak1.cpp(44) : {98} client block at 0x00394B30, subtype 0, 8 bytes long.
 Data: <xK9  K9 > 78 4B 39 00 B8 4B 39 00
g:/windows核心编程代码/testmemoryleak/testmemoryleak1/testmemoryleak1.cpp(25) : {97} normal block at 0x00397FE0, 12 bytes long.
 Data: <            > CD CD CD CD CD CD CD CD CD CD CD CD
g:/windows核心编程代码/testmemoryleak/testmemoryleak1/testmemoryleak1.cpp(25) : {96} client block at 0x00397FA0, subtype 0, 4 bytes long.
 Data: <    > CD CD CD CD
g:/windows核心编程代码/testmemoryleak/testmemoryleak1/testmemoryleak1.cpp(42) : {95} normal block at 0x00397F00, 100 bytes long.
 Data: <m               > 6D CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
g:/windows核心编程代码/testmemoryleak/testmemoryleak1/testmemoryleak1.cpp(37) : {93} normal block at 0x00397EB8, 10 bytes long.
 Data: <          > CD CD CD CD CD CD CD CD CD CD
g:/windows核心编程代码/testmemoryleak/testmemoryleak1/testmemoryleak1.cpp(35) : {92} client block at 0x00397E78, subtype 0, 4 bytes long.
 Data: <    > CD CD CD CD
Object dump complete.

 

上面的结果可以在DebugView中查看!或是按F10(F5)到

 mainret = wmain(argc, argv, envp);
#else  /* WPRFLAG */
            __initenv = envp;
            mainret = main(argc, argv, envp);
#endif  /* WPRFLAG */

#endif  /* _WINMAIN_ */

            /*
             * Note that if the exe is managed app, we don't really need to
             * call exit or _c_exit. .cctor should be able to take care of
             * this.
             */
            if ( !managedapp )
                exit(mainret);
时,也可以在输出窗口看到。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值