在 MFC 中,可以使用 DEBUG_NEW 宏代替 new 运算符来帮助定位内存泄漏。在程序的“Debug”版本中,DEBUG_NEW 将为所分配的每个对象跟踪文件名和行号。当编译程序的“Release”版本时,DEBUG_NEW 将解析为不包含文件名和行号信息的简单 new 操作。因此,在程序的“Release”版本中不会造成任何速度损失。如果不想重写整个程序来使用 DEBUG_NEW 代替 new,则可以在源文件中定义下面的宏:#define new DEBUG_NEW 当进行对象转储时,用 DEBUG_NEW 分配的每个对象均将显示被分配到的文件和行号,使您可以查明内存泄漏源。MFC 框架的“Debug”版本自动使用 DEBUG_NEW,但代码不自动使用它。如果希望利用 DEBUG_NEW 的好处,则必须显式使用 DEBUG_NEW 或 #define new,如上所示。
Expands to the name of the file that is being compiled.THIS_FILE Res The information is used by the ASSERT and VERIFY macros. The Application Wizard and code wizards place the macro in source code files they create.Example #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif// __FILE__ is one of the six predefined ANSI C macros that the // compiler recognizes.
#pragma once
差不多功能
当进行对象转储时,用 DEBUG_NEW 分配的每个对象均将显示被分配到的文件和行号,使您可以查明内存泄漏源。MFC 框架的“Debug”版本自动使用 DEBUG_NEW,但代码不自动使用它。如果希望利用 DEBUG_NEW 的好处,则必须显式使用 DEBUG_NEW 或 #define new,如上所示。
Res
The information is used by the ASSERT and VERIFY macros. The Application Wizard and code wizards place the macro in source code files they create.Example
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif// __FILE__ is one of the six predefined ANSI C macros that the
// compiler recognizes.