PRB:Microsoft Foundation Classes DEBUG_NEW Does Not Work withGDI+
ArticleID : 317799
Last Review : February 12,2007
Revision : 2.1
This article was previously published underQ317799
SYMPTOMS
When you build a debug version of a Microsoft Foundation Classes(MFC) application that uses GDI+, you may receive an error messagethat resembles the following:
error C2660: 'Gdiplus::GdiplusBase::operatornew' : function does not take 3 parameters
CAUSE
In debugbuilds, MFC defines a preprocessor macro that expands the newoperator to an overloaded new operator that takes two extraparameters. The extra parameters are the source file name and codeline number. MFC can use this information to report memory leaks tothe programmer when in debug mode. This works for MFC classesbecause MFC provides overloads for new that accept the extraparameters.
However, because this expansion is done by thepreprocessor, it affects all usage of the new operator. If anynon-MFC classes are used in the project, their new operator is alsoexpanded, even if no suitable overload of new is available in thatclass. This is what happens in GDI+, and as a result, you receive acompile-time error message.
WORKAROUND
To workaround this problem, choose one of the followingmethods:
" Turn off the preprocessor expansion bycommenting out the following lines of code in the sourcefile:
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
NOTE: This method has the disadvantage of notusing features in MFC that help you track memory allocations andleaks.
" Provide GDI+ with overloads for new and deleteoperators by writing some code that accepts and discards theadditional parameters. You can paste the following code, whichdemonstrates this approach, into a new header file and include thenew header file instead of the Gdiplus.h file.
//// Ensure that GdiPlus header files workproperly with MFC DEBUG_NEW and STL header files.
#define iterator _iterator
#ifdef _DEBUG
namespace Gdiplus
{
namespace DllExports
{
#include
};
#ifndef_GDIPLUSBASE_H
#define _GDIPLUSBASE_H
class GdiplusBase
{
public:
void(operator delete)(void* in_pVoid)
{
DllExports::GdipFree(in_pVoid);
}
void*(operator new)(size_t in_size)
{
returnDllExports::GdipAlloc(in_size);
}
void(operator delete[])(void* in_pVoid)
{
DllExports::GdipFree(in_pVoid);
}
void*(operator new[])(size_t in_size)
{
returnDllExports::GdipAlloc(in_size);
}
void* (operator new)(size_t nSize, LPCSTR lpszFileName, intnLine)
{
returnDllExports::GdipAlloc(nSize);
}
voidoperator delete(void* p, LPCSTR lpszFileName, int nLine)
{
DllExports::GdipFree(p);
}
};
#endif // #ifndef _GDIPLUSBASE_H
}
#endif // #ifdef _DEBUG
#include
#undef iterator
//// Ensure that Gdiplus.lib is linked.
#pragma comment(lib, "gdiplus.lib")