new FontFamily失败??!! (2006-06-09 18:02)
分类:
GDI+
FontFamily *pff = new FontFamily;
error C2660: 'new' : function does not take 3 parameters
FontFamily *pff = new FontFamily[5];
error C2660: 'new[]' : function does not take 3 parameters
FontFamily *pff = new FontFamily(L"Asia");
error C2660: 'new' : function does not take 3 parameters
FontFamily *pff = new FontFamily(L"Asia")[3];
error C2660: 'new' : function does not take 3 parameters
error C2143: syntax error : missing ';' before '['
真不知道FontFamily类是怎么作出这种效果的
------------------------------------------------------------------------------------------------------------------------------------------
终于google出原因是:
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
详细的解释在这里:
核心是说DEBUG模式下有
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
因此new被重载为
#if defined(_DEBUG) && !defined(_AFX_NO_DEBUG_CRT)
void * AFX_CDECL operator new (size_t nSize , LPCSTR lpszFileName , int nLine );
#define DEBUG_NEW new(THIS_FILE, __LINE__)
#if _MSC_VER >= 1200
void AFX_CDECL operator delete ( void * p , LPCSTR lpszFileName , int nLine );
#endif
void * AFX_CDECL operator new (size_t nSize , LPCSTR lpszFileName , int nLine );
#define DEBUG_NEW new(THIS_FILE, __LINE__)
#if _MSC_VER >= 1200
void AFX_CDECL operator delete ( void * p , LPCSTR lpszFileName , int nLine );
#endif
DEBUG_NEW默认有3个参数, 而GDI+中
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 );
}
};
{
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 );
}
};
没有匹配的operator new在RELEASE模式下不会出问题(没有重载new)
要在DEBUG模式下编译通过只需给Gdiplusbase.h增加匹配的operator new
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 );
}
{
return DllExports ::GdipAlloc (nSize );
}
void (operator delete) ( void * p , LPCSTR lpszFileName , int nLine )
{
DllExports ::GdipFree (p );
}
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);
}
{
return DllExports::GdipAlloc(nSize);
}
void (operator delete[])(void* p, LPCSTR lpszFileName, int nLine)
{
DllExports::GdipFree(p);
}
------------------------------------------------------------------------------------------------------------------------------------------
如果 PrivateFontCollection 和 FontFamily 都是 new 出来的
delete 要先 FontFamily 后 PrivateFontCollection 否则会异常