GDI+下使用DEBUG_NEW问题

在WTL中使用GDI+,出现编译问题:

错误 C2660: Gdiplus::GdiplusBase::operator 新: 函数不采用 3 的参数
error C2660: 'new' : function does not take 3 parameters

搜索后发现Microsoft上有相关的support说明
http://support.microsoft.com/kb/317799

原因在于:
在调试版本中 MFC 定义重载 新 运算符带有两个额外的参数来扩展 new 运算符的预处理器宏。 额外的参数都是源代码文件的名称和代码行号。 MFC 可以使用此信息对程序员在调试模式下时报告内存泄漏。 这适用于 MFC 类因为 MFC 提供的 新 接受额外的参数的重载。

但是,此展开由预处理器完成,因为它会影响所有使用 new 运算符。 如果在项目中使用任何非 MFC 类,其 new 运算符是也即使的 新 没有适合重载是在该类中的可用扩展。 这是怎样 GDI + 中,您会收到一个编译时错误消息作为结果。

采用他建议的方法,添加下面这个头文件就可解决。
<textarea cols="50" rows="15" name="code" class="cpp">/* ------------------------------------------------------------------------- // FileName : kgdiplus.h // Creator : linyehui // Date : 2009/10/28 10:20:32 // Brief : http://support.microsoft.com/kb/317799 // // $Id: $ // -----------------------------------------------------------------------*/ #ifndef __KGDIPLUS_H__ #define __KGDIPLUS_H__ // ------------------------------------------------------------------------- //// 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 &lt;GdiplusMem.h&gt; }; #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); } void * (operator new)(size_t nSize, int nNormalBlockType, LPCSTR lpszFileName, int nLine) { return DllExports::GdipAlloc(nSize); } void operator delete(void* p, int nNormalBlockType, LPCSTR lpszFileName, int nLine) { DllExports::GdipFree(p); } }; #endif // #ifndef _GDIPLUSBASE_H } #endif // #ifdef _DEBUG #include &lt;gdiplus.h&gt; #undef iterator //// Ensure that Gdiplus.lib is linked. #pragma comment(lib, "gdiplus.lib") // ------------------------------------------------------------------------- // $Log: $ #endif /* __KGDIPLUS_H__ */ </textarea>

 

使用时:

#include "kgdiplus.h"
using namespace Gdiplus;

好了,你又可以使用:

 <textarea cols="50" rows="15" name="code" class="cpp:nogutter:nocontrols">#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif</textarea>

 

很久之前的笔记,备忘。

The End

 

你可能感兴趣的:(GDI+下使用DEBUG_NEW问题)