3

我有一个内存泄漏,我正试图在我的 mfc 程序中寻找它。通常,我会执行以下操作:

头文件

// Leak Detection
#if defined(WIN32) && defined(_DEBUG)
     #define _CRTDBG_MAP_ALLOC
     #include <stdlib.h>
     #include <crtdbg.h>
#endif

.cpp 文件

// Leak detection
#if defined(WIN32) && defined(_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
    #ifdef DEBUG_NEW 
        #undef DEBUG_NEW
    #endif
    #define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )
    #define new DEBUG_NEW
#endif

这种技术适用于大多数文件,但是当我将它包含在某些文件(例如我的文档)中时,我收到错误:错误 C2661:'CObject::operator new':没有重载函数需要 4 个参数

这里有什么解决方案?我应该在某个地方#undef-ing 新事物吗?

谢谢!

4

2 回答 2

1

我也使用与您相同的功能来进行泄漏检测。

假设您不再需要它来捕获内存泄漏,您可以注释掉或删除 DEBUG_NEW 定义块。或者,如果您仍然需要它,请保持原样并使用

#ifdef _DEBUG
#undef new
    CMyOject* pMyObjectInst = new CMyObject();
#define new DBG_NEW
#endif  

因此,您在创建对象之前取消定义 new(请参阅错误列表中的行号)并在之后立即重新定义它,以便在此对象创建之后发生的任何内存泄漏仍然可以识别。

于 2012-10-16T12:15:14.830 回答
1

我在 .cpp 文件中放置了#define new DEBUG_NEWbefore语句导致了类似的问题。#include ...更改顺序解决了我的问题。

于 2013-07-18T08:23:10.573 回答