简体   繁体   中英

Memory usage increases with DrawText in Win32 C++

I have implemented a simple game using MFC. It contains images and formatted text. While testing, I found out that after the program runs for about 7 minutes without a problem, after that the images disappear and the text formatting resets. To identify the problem I rewrote a very basic version which only shows a formatted text. But the problem still continues, I have been googling for a day now but the problem is still the same. Here is my OnPaint method

    CPaintDC dc(this);

    CRect clientRect;
    GetClientRect(&clientRect);

    CDC bitmapDC;
    bitmapDC.CreateCompatibleDC(&dc);

    CBitmap bitmap;
    bitmap.CreateCompatibleBitmap(&dc, clientRect.Width(), clientRect.Height());
    bitmapDC.SelectObject(&bitmap);

    CBrush backgroundBrush;
    backgroundBrush.CreateSolidBrush(RGB(0xf8, 0xf8, 0xf8));
    bitmapDC.FillRect(&clientRect, &backgroundBrush);

    int SavedDc = bitmapDC.SaveDC();

    long nHeight = -MulDiv(20, GetDeviceCaps(bitmapDC, LOGPIXELSY), 72);
    HFONT myfont = CreateFont(nHeight, 0, 0, 0, FW_ULTRALIGHT, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS,
        CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, VARIABLE_PITCH, TEXT("Arial"));

    bitmapDC.SelectObject(myfont);
    SetBkMode(bitmapDC, TRANSPARENT);

    bitmapDC.DrawText(L"This shouldn't happen", &clientRect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);

    dc.BitBlt(0, 0, clientRect.Width(), clientRect.Height(), &bitmapDC, 0, 0, SRCCOPY);

    bitmapDC.RestoreDC(SavedDc);
    backgroundBrush.DeleteObject();
    bitmap.DeleteObject();
    bitmapDC.DeleteDC();

I can see that the memory usage is increased gradually in visual studio. If I comment out the DrawText part ít does not increase any more. Am I doing something wrong? How can I keep the DrawText method from increasing memory usage? I have also tested with an image and it is the same problem. Should I somehow release or delete the DrawText? Any help would be much appreciated.

Disappearing images and fonts are a sign of leaked GDI resources, not the memory.

While it's pretty bad to leak memory, it's worse to leak resources, as they are pretty limited. Observe the GDI Objects column in the Task Manager for your game; when the counter hits 10,000 - you are done.

It is NOT enough to delete your MFC object holding the GDI resource, you need to first de-select them from DC (the currently selected object can not be released).

Just in case the objects selected into a DC are not able to delete, why don't you process strictly?

bitmapDC.SelectObject(&bitmap);
 ↓
CBitmap* pbmold = bitmapDC.SelectObject(&bitmap);

(omission)

bitmapDC.SelectObject(pbmold); // addition
bitmap.DeleteObject();
bitmapDC.DeleteDC();

font is also the same as that...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM