Microsoft Foundation Classes DEBUG_NEW Does Not Work with GDI+

本文介绍了解决Microsoft Foundation Classes (MFC) 应用在使用 GDI+ 时出现的新操作符参数错误的方法。通过禁用预处理器宏或为 GDI+ 提供新的操作符重载来解决此问题。

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...

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 p...

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:
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    						
    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.
    
    #define iterator _iterator
    
    #ifdef _DEBUG
    
    namespace Gdiplus
    {
    	namespace DllExports
    	{
    		#include <GdiplusMem.h>
    	};
    
    	#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
    }
    #endif // #ifdef _DEBUG
    
    #include <gdiplus.h>
    #undef iterator
    //// Ensure that Gdiplus.lib is linked.
    #pragma comment(lib, "gdiplus.lib")
    					

STATUS
This behavior is by design.

This behavior is by design.

APPLIES TO
  • Microsoft GDI+ 1.0
  • Microsoft Windows XP Professional
Keywords: 
kbprb kbdswgdi2003swept KB317799
 
Provide feedback on this information
Did this information solve your problem?
Yes
No
I don't know
Was this information relevant?
Yes
No
What can we do to improve this information?
To protect your privacy, do not include contact information in your feedback.

Article Translations

 

Related Support Centers

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值