VC里的"#define new DEBUG_NEW"
2013-06-26 12:02
309 查看
以下代码常常在一个类文件的开头出现,是什么意思呢?
先看MSDN里的解释:
Remarks
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.
这样就很清楚了,当在debug模式下时,我们分配内存时的new被替换成DEBUG_NEW,而这个DEBUG_NEW不仅要传入内存块的大小,还要传入源文件名和行号,这就有个好处,即当发生内存泄漏时,我们可以在调试模式下定位到该问题代码处。若删掉该句,就不能进行定位了。而在release版本下的new就是简单的new,并不会传入文件名和行号。
如果定义了_DEBUG,表示在调试状态下编译,因此相应修改了两个符号的定义THIS_FILE是一个char数组全局变量,字符串值为当前文件的全路径,这样在Debug版本中当程序出错时出错处理代码可用这个变量告诉你是哪个文件中的代码有问题。
#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif
先看MSDN里的解释:
Remarks
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.
这样就很清楚了,当在debug模式下时,我们分配内存时的new被替换成DEBUG_NEW,而这个DEBUG_NEW不仅要传入内存块的大小,还要传入源文件名和行号,这就有个好处,即当发生内存泄漏时,我们可以在调试模式下定位到该问题代码处。若删掉该句,就不能进行定位了。而在release版本下的new就是简单的new,并不会传入文件名和行号。
如果定义了_DEBUG,表示在调试状态下编译,因此相应修改了两个符号的定义THIS_FILE是一个char数组全局变量,字符串值为当前文件的全路径,这样在Debug版本中当程序出错时出错处理代码可用这个变量告诉你是哪个文件中的代码有问题。
相关文章推荐
- VC里的"#define new DEBUG_NEW"
- 关于"#define new DEBUG_NEW"
- VC里的"#define new DEBUG_NEW"
- VC|MFC内存不能为"read",内存不能为 "written" 分析
- "o_neww_ww_123"分割字符串split
- VC|MFC内存不能为"read"的调试方法
- "new types may not be defined in a return type" error
- VC|MFC内存不能为"read"
- 解决Xcode 4.3.2的"Could not insert new outlet connection"问题
- 对于String对象,可以使用"="赋值,也可以使用"new"关键字赋值,两种方式有什么区别?
- 关于"#define new DEBUG_NEW"
- VC|MFC内存不能为"read",内存不能为 "written" 分析
- 转:关于VC中的"stdafx.h"
- VC编译错误:uafxcw.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) 已经在 LIBCMT.lib(new.obj
- 关于"#define new DEBUG_NEW"
- VC编译错误:uafxcw.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPA
- VC|MFC内存不能为"read"+仅通过崩溃地址找出源代码的出错行
- "无法启动应用程序,工作组信息文件丢失,或是已被其他用户已独占方式打开"在VC中解决办法
- "operator new" 和"new operator "
- 解决Xcode 4.3.2的"Could not insert new outlet connection"问题