VC++中本身就有内存泄漏检查的机制,可以在向导生成的支持MFC的工程中看到如下代码:
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
先具体解释一下:
#ifdef _DEBUG //如果有定义_DEBUG
#define new DEBUG_NEW //将new宏定义成DEBUG_NEW,
那么则是代码中有new的都换成DEBUG_NEW。
#undef THIS_FILE//取消THIS_FILE的宏定义
static char THIS_FILE[] = __FILE__;//将THIS_FILE
定义成一个数组,该数组用static声名,则只能在该文件内访问。__FILE__是gcc定义的一个扩展宏,代表的该文件的文件名。
#endif//这个不用说了吧
MFC Library中的解释
THIS_FILESee Also
MFC Macros and Globals | ASSERT | VERIFYExpands to the name of the file that is being compiled.
THIS_FILE
Remarks
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
MFC Macros and Globals | ASSERT | VERIFYExpands to the name of the file that is being compiled.
THIS_FILE
Remarks
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.
// compiler recognizes.