#define new DEBUG_NEW...
来源:互联网 发布:java基础电影售票系统 编辑:程序博客网 时间:2024/05/17 23:51
在用vc时,利用AppWizard会产生如下代码:
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
对于#define new DEBUG_NEW
首先看msdn的解释:
Assists in finding memory leaks.You can use DEBUG_NEW everywhere in your program that you would ordinarily use the new operator to allocate heap storage.
In debug mode (when the _DEBUG symbol is defined), DEBUG_NEW keeps track of the filename and line number for each object that it allocates. Then, when you use the CMemoryState::DumpAllObjectsSince member function, each object allocated with DEBUG_NEW is shown with the filename and line number where it was allocated.
To use DEBUG_NEW, insert the following directive into your source files:
#define new DEBUG_NEW
Once you insert this directive, the preprocessor will insert DEBUG_NEW wherever you use new, and MFC does the rest. When you compile a release version of your program, DEBUG_NEW resolves to a simple new operation, and the filename and line number information is not generated.
再查看定义:
#ifdef _DEBUG
void* AFX_CDECL operator new(size_t nSize, LPCSTR lpszFileName, int nLine);
#define DEBUG_NEW new(THIS_FILE, __LINE__)
#else
#define DEBUG_NEW new
#endif
这样就很清楚了,当在debug模式下时,我们分配内存时的new被替换成DEBUG_NEW,而这个DEBUG_NEW不仅要传入内存块的大小,还要传入源文件名和行号,这就有个好处,即当发生内存泄漏时,我们可以在调试模式下定位到该问题代码处。若删掉该句,就不能进行定位了。而在release版本下的new就是简单的new,并不会传入文件名和行号。
因此,我们在开发代码阶段,保留上述代码是值得的。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Sco_field/archive/2008/12/17/3537543.aspx
- #define new DEBUG_NEW
- 关于#define new DEBUG_NEW
- #define new DEBUG_NEW...
- #define new DEBUG_NEW
- #define new DEBUG_NEW
- #define new DEBUG_NEW
- #define new DEBUG_NEW 作用
- #define new DEBUG_NEW
- #define new DEBUG_NEW解释
- #define new DEBUG_NEW
- #define new DEBUG_NEW
- #define new DEBUG_NEW 作用?
- 关于#define new DEBUG_NEW
- 关于"#define new DEBUG_NEW"
- #define new DEBUG_NEW
- #define new DEBUG_NEW
- #define new DEBUG_NEW
- #define new DEBUG_NEW
- VB如何将数据导入WORD模版打印
- 新手安装symbian开发环境
- OCX调用方法
- C语言代码优化方案
- 识别CSDN的验证码
- #define new DEBUG_NEW...
- Struts2 AJAX 验证
- ASP.NET调用存储过程两种方法浅析
- C#面试题
- FOJ 1106 Sum of Factorials
- 周末无事
- 大量数据优化常用方法
- 十七大之后我国经济社会发展的基本趋势
- C#里面的string和String的区别