C++代码运行时间长了以后就会崩溃,是GDI函数释放问题吗?
本帖最后由 VisualEleven 于 2013-09-05 11:16:52 编辑
void CALLBACK CRealPlayer::registerDrawFun( long nPort,HDC hDc,LONG nUser )gdi c++ 字体
{
CRealPlayer* realPlayer = reinterpret_cast<CRealPlayer*>( nUser );
if ( !realPlayer)
{
std::cout << "realPlayer is NULL!!" << std::endl;
return;
}
HWND wind;
realPlayer->getWinId( wind );
if ( NULL == wind )
{
std::cout << "wind is NULL!!" << std::endl;
return;
}
RECT rec;
GetClientRect( wind,&rec );
unsigned long TextColor = realPlayer->m_Osdinfo.m_fontColor;
SetTextColor( hDc, TextColor );
SetBkMode( hDc,TRANSPARENT );
uint32_t ret = rec.bottom/155;
int px = realPlayer->m_Osdinfo.m_xPos,py = realPlayer->m_Osdinfo.m_yPos;
px = px * rec.right / 190;
py = py * rec.bottom / 155;
LOGFONT logfont; //改变输出字体
ZeroMemory( &logfont,sizeof( LOGFONT ) );
logfont.lfCharSet = GB2312_CHARSET;
logfont.lfHeight = MulDiv(10*ret,GetDevice(hDc,LOGPIXELSY),72);
HFONT hFont = CreateFontIndirect(&logfont);
if ( NULL == hFont )
{
DWORD err = GetLastError();
std::cout << err << " hFont is NULL!!" << std::endl;
return;
}
SelectObject(hDc, hFont);
size_t size = realPlayer->m_Osdinfo.m_osdText.length();
wchar_t *buffer = new wchar_t[ size/2 + 1];
MultiByteToWideChar( CP_ACP, 0, hikPlayer->m_Osdinfo.m_osdText.c_str(), size, buffer, size );
buffer[size/2] = 0;
TextOut(hDc,px,py,buffer,size/2 );
DeleteObject( hFont );
DeleteDC(hDc);
}
[解决办法]
new delete
其它的看不出来。。
你看看任务管理器--句柄数是多少
[解决办法]
你的DeleteObject( hFont ); 操作应该会失败,
你需要在它之前调用SelectObject(hOldFont);加载旧的HFONT
[解决办法]
HFONT* oldFont = &hFont;
SelectObject(hDc, hFont);
--》
HFONT oldFont = (HFONT)SelectObject(hDc, hFont);