gpt4 book ai didi

c++ - BitBlt 内存泄漏

转载 作者:太空宇宙 更新时间:2023-11-04 16:08:50 25 4
gpt4 key购买 nike

我正在使用 BitBlt 在我的按钮上显示位图。对于大多数情况,它很好,但是存在内存泄漏,导致程序在一段时间后崩溃。我做错了什么?

int Springboard::DrawBasicButtons(DRAWITEMSTRUCT* pdis, HINSTANCE hInstance){
RECT rect;
static HBITMAP hCurrIcon, hIconoff, hIconon;
rect = pdis->rcItem;

HFONT font = CreateFont(13, 0, 0, 0, 300, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, L"Arial");
TCHAR Txtstr[MAX_PATH];
BOOL isText = FALSE;
int textsize;

if (IDC_HOLD == pdis->CtlID) {
hIconoff = (HBITMAP) LoadBitmap(hInstance, MAKEINTRESOURCE(BASIC_HOLDOFF));
hIconon = (HBITMAP) LoadBitmap(hInstance, MAKEINTRESOURCE(BASIC_HOLDON));
_tcscpy( Txtstr, _T("Hold "));
isText = TRUE;
if (pdis->itemState & ODS_SELECTED) hCurrIcon = hIconon;
else hCurrIcon = hIconoff;
}

HDC hdc = CreateCompatibleDC(pdis->hDC);
SelectObject(hdc, hCurrIcon);

BitBlt(pdis->hDC,0, 0,ICON_WIDTH,ICON_HEIGHT, hdc, 0, 0, SRCCOPY);

if(isText == TRUE){
textsize = _tcslen(Txtstr);
SetTextColor(pdis->hDC, RGB(230,230,230));
HFONT hFontOld = (HFONT) SelectObject(pdis->hDC, font);
DrawText(pdis->hDC, Txtstr, textsize, &pdis->rcItem, DT_SINGLELINE | DT_VCENTER | DT_RIGHT);
SelectObject( pdis->hDC, hFontOld );
}

DeleteDC(hdc);
DeleteBitmap(hCurrIcon);
DeleteBitmap(hIconoff);
DeleteBitmap(hIconon);
font = NULL;

return(RET_OK);
}

最佳答案

在调用 DeleteDC() 之前,您需要将旧对象选回 HDC

此外,您似乎没有清理 CreateFont() 返回的 HFONT

关于c++ - BitBlt 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31052482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com