C++ 内存泄露 截获 宏相关

lonelyrains 2011-07-05 09:03:10


#include <crtdbg.h>

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

//#define _CRTDBG_MAP_ALLOC

#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif

//直接#define new new(_CLIENT_BLOCK,__FILE__,__LINE__)报错
void _EnCheckMemoryLeaks()
{
int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag( tmpFlag );
//改成_CrtSetDbgFlag( _CRTDBG_REPORT_FLAG|_CRTDBG_LEAK_CHECK_DF );不能打印内存泄露
}

int main(int argc,char* argv[])
{
_EnCheckMemoryLeaks();
int *p=new int();
//_CrtDumpMemoryLeaks();
delete p;
return 0;
}



参考http://blog.csdn.net/phinecos/article/details/4745720的文章,写的代码。对其中的宏定义很不理解。为什么

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

//#define _CRTDBG_MAP_ALLOC

#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif

定义为

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

或者

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

都会报错呢?C++不是有防止宏递归的机制吗?而且为什么分成两部分就不报错了呢?
...全文
116 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wulala456 2011-07-05
  • 打赏
  • 举报
回复
第一次运行有错误,再运行一次就没错了.
wulala456 2011-07-05
  • 打赏
  • 举报
回复
http://wanliqunn.blog.163.com/blog/static/9865179200811195386572/

另:VS2005上你举的三个都没有报错,都很正常.

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

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



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



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

int _tmain(int argc, _TCHAR* argv[])
{
int* p = new int();
_CrtDumpMemoryLeaks();
return 0;
}


taodm 2011-07-05
  • 打赏
  • 举报
回复
换一个编译器再试。
lonelyrains 2011-07-05
  • 打赏
  • 举报
回复
1楼是个mm?访问量貌似还挺高
[Quote=引用 1 楼 tan625747 的回复:]
不清楚,等明天上班,有高人来解答吧
[/Quote]
lonelyrains 2011-07-05
  • 打赏
  • 举报
回复

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


就行了,貌似还是那个宏没有完全展开的原因。这个真有点头大,没法调试,所以很难找引起错误的关键位置在哪
tan625747 2011-07-05
  • 打赏
  • 举报
回复
不清楚,等明天上班,有高人来解答吧

684

社区成员

发帖
与我相关
我的任务
社区描述
智能路由器通常具有独立的操作系统,包括OpenWRT、eCos、VxWorks等,可以由用户自行安装各种应用,实现网络和设备的智能化管理。
linuxpython 技术论坛(原bbs)
社区管理员
  • 智能路由器社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧