Microsoft KB Archive/317799

= PRB: Microsoft Foundation Classes DEBUG_NEW Does Not Work with GDI+ =

Article ID: 317799

Article Last Modified on 2/12/2007

-

APPLIES TO


 * Microsoft GDI+ 1.0
 * Microsoft Windows XP Professional
 * Microsoft Windows XP Professional for Itanium-based systems

-



This article was previously published under Q317799



SYMPTOMS
When you build a debug version of a Microsoft Foundation Classes (MFC) application that uses GDI+, you may receive an error message that resembles the following:

error C2660: 'Gdiplus::GdiplusBase::operator new' : function does not take 3 parameters



CAUSE
In debug builds, MFC defines a preprocessor macro that expands the new operator to an overloaded new operator that takes two extra parameters. The extra parameters are the source file name and code line number. MFC can use this information to report memory leaks to the programmer when in debug mode. This works for MFC classes because MFC provides overloads for new that accept the extra parameters.

However, because this expansion is done by the preprocessor, it affects all usage of the new operator. If any non-MFC classes are used in the project, their new operator is also expanded, even if no suitable overload of new is available in that class. This is what happens in GDI+, and as a result, you receive a compile-time error message.



WORKAROUND
To work around this problem, choose one of the following methods:   Turn off the preprocessor expansion by commenting out the following lines of code in the source file: NOTE: This method has the disadvantage of not using features in MFC that help you track memory allocations and leaks.   Provide GDI+ with overloads for new and delete operators by writing some code that accepts and discards the additional parameters. You can paste the following code, which demonstrates this approach, into a new header file and include the new header file instead of the Gdiplus.h file. //// Ensure that GdiPlus header files work properly with MFC DEBUG_NEW and STL header files.
 * 1) ifdef _DEBUG
 * 2) define new DEBUG_NEW
 * 3) endif


 * 1) define iterator _iterator


 * 1) 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) {               return DllExports::GdipAlloc(in_size); }

void (operator delete[])(void* in_pVoid) {               DllExports::GdipFree(in_pVoid); }

void* (operator new[])(size_t in_size) {               return DllExports::GdipAlloc(in_size); }

void * (operator new)(size_t nSize, LPCSTR lpszFileName, int nLine) {               return DllExports::GdipAlloc(nSize); }

void operator delete(void* p, LPCSTR lpszFileName, int nLine) {               DllExports::GdipFree(p); }

};   #endif // #ifndef _GDIPLUSBASE_H }
 * 1) endif // #ifdef _DEBUG

//// Ensure that Gdiplus.lib is linked. 
 * 1) include 
 * 2) undef iterator
 * 1) pragma comment(lib, &quot;gdiplus.lib&quot;)



STATUS
This behavior is by design.

Keywords: kbdswgdi2003swept kbprb KB317799

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.