gdi+在Graphics遇到的new 问题。

来源:互联网 发布:mac ios模拟器 玩游戏 编辑:程序博客网 时间:2024/06/01 10:46

如果在gdi+中使用 如下:Graphics * graphics = new Graphics(pDc->m_hDC); 那么它就会报错,原因如下:

http://support.microsoft.com/kb/317799/en-us 。。 是因为在调试版本下,mfc的宏扩展,将new 运算符扩展为三个参数, 另外两个额外参数分别是source file name 和code line numbe。如果使用非mfc类,就会出现问题,所以在新类中还要重载如下new 和delete :

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);
}
即可

 

方法,可以在 gdi+的源代码 graphicsbase.h 中补上

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);
}
即可

ps: 如果删除 Graphics 对象,则应该在  GdiplusShutdown(gdiplusToken); 之前,否则报错。如下

 delete m_graphics;
 GdiplusShutdown(gdiplusToken);