Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4474087
  • 博文数量: 356
  • 博客积分: 10458
  • 博客等级: 上将
  • 技术积分: 4734
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-24 14:59
文章分类

全部博文(356)

文章存档

2020年(17)

2019年(9)

2018年(26)

2017年(5)

2016年(11)

2015年(20)

2014年(2)

2013年(17)

2012年(15)

2011年(4)

2010年(7)

2009年(14)

2008年(209)

分类: C/C++

2008-05-08 20:50:03

怎样检查你的代码有没有内存泄露呢? 需要在前面加上这些代码:

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

#define _CRTDBG_MAP_ALLOC
#include
#include
#include
#include

#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif

然后在要检测的代码前面加上:
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
在后面加上:
_CrtDumpMemoryLeaks();


如:

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

#define _CRTDBG_MAP_ALLOC
#include
#include
#include
#include

#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif
int main()
{
    _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
    int *p;
    p = new int;
    _CrtDumpMemoryLeaks();
    return 0;
}

然后按F5进行调试,会发现输出框中显示:
Detected memory leaks!
阅读(3167) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2008-05-20 21:48:16

晕..... 楼上到底有没有看完此贴 你看最后面那个是啥

chinaunix网友2008-05-20 16:06:02

转载请注明出处!