问题
I have a memory leak that I'm trying to hunt down in my mfc program. Typically I would do something like the following:
header file
// Leak Detection
#if defined(WIN32) && defined(_DEBUG)
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif
cpp file
// 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
This technique works well in most files, but when I include it in some files such as my document, I get the error: error C2661: 'CObject::operator new' : no overloaded function takes 4 arguments
What's the solution here? Should I be #undef-ing new somewhere or something?
Thanks!
回答1:
I also use the same functionality as you for the purpose of leak detection.
Either you can comment out or delete the DEBUG_NEW definition block, assuming you don't need it any more for trapping memory leaks. Or if you still need it, leave it as it is and use
#ifdef _DEBUG
#undef new
CMyOject* pMyObjectInst = new CMyObject();
#define new DBG_NEW
#endif
So, you undefine new just before object creation (see the line numbers in your Error List) and redefine it again immediately after, so that any memory leaks which occur after this object creation are still identifiable.
回答2:
I have similar problem caused by putting #define new DEBUG_NEW
before #include ...
statements in .cpp file. Changing the order resolved my problem.
来源:https://stackoverflow.com/questions/10952731/error-c2661-cobjectoperator-new-no-overloaded-function-takes-4-arguments