c++ - HDC内存泄漏(释放HDC/删除hdc)

标签 c++ memory-management memory-leaks gdi

我遇到了这个 HDC 内存泄漏问题。你们能检查一下我是否正确地释放/删除了 HDC 吗?

谢谢!

BITMAP bm;
HBITMAP hbmap;
HBITMAP hBitmapOld;
BITMAPINFO bmi;
HDC hdcShot;

...

while(true)
{
    if (!TakeScreenshot(GameWindow, bm, hbmap, bmi, hdcShot, hBitmapOld, appWnd))
                    break;

        HBITMAP hbmapNew = CreateCompatibleBitmap(hdcShot, rcWindow.right - rcWindow.left, rcWindow.bottom - rcWindow.top);

        HDC hdcShotNew = CreateCompatibleDC(hdcShot);

        HBITMAP OldBmp = (HBITMAP)SelectObject(hdcShotNew, hbmapNew);


        BitBlt(hdcShotNew, 0, 0, rcWindow.right - rcWindow.left/*Window WIDTH*/, rcWindow.bottom - rcWindow.top/*Window HEIGHT*/
            , hdcShot, 0, 0, SRCCOPY);


        pPixels = new RGBQUAD[bm.bmWidth * bm.bmHeight];
        if (!pPixels) return false;

        SelectObject(hdcShotNew, OldBmp);


        if (!GetDIBits(hdcShotNew, hbmapNew, 0, bm.bmHeight, pPixels, &bmi, DIB_RGB_COLORS))
        {
            DeleteDC(hdcShot);
            delete[] pPixels;

            return false;
        }



// dont mind about this
        ScanContents scanContentsMain(bm, rcWindow, pPixels);
// dont mind about this
        ScanBMPHorizontal(&scanContentsMain);




        //free memory - I might have cleared the memory incorrectly here! Please check!
        free(pPixels);
        SelectObject(hdcShot, hBitmapOld);
                DeleteDC(hdcShot);
                DeleteObject(hbmapNew);
                DeleteDC(hdcShotNew);
}

TakeScreenShot Func(不是很重要,但它显示了一些变量是如何初始化的)

    bool TakeScreenshot(std::string WindowToFind, BITMAP &bm, HBITMAP &hbmap, BITMAPINFO &bmi, HDC &hdcShot, HBITMAP &hBitmapOld, HWND &hwnd)
{
    RECT rc;
    GetWindowRect(hwnd, &rc);
    hdcShot = CreateCompatibleDC(0);
    hbmap = CreateCompatibleBitmap(GetDC(0), rc.right - rc.left/*Window WIDTH*/, rc.bottom - rc.top/*Window HEIGHT*/);

    SelectObject(hdcShot, hbmap);


    BitBlt(hdcShot, 0, 0, rc.right - rc.left/*Window WIDTH*/, rc.bottom - rc.top/*Window HEIGHT*/
        , GetDC(0), rc.left, rc.top, SRCCOPY);

//Ignore this
    if (!GetObject(hbmap, sizeof(BITMAP), (LPSTR)&bm))
        return false;
    int bitsPerPixel = bm.bmBitsPixel;


 //Ignore this  
    if (bitsPerPixel != 32 || bm.bmPlanes != 1)
        return false;

//Don't mind about this too much 
    SetupBitmapInfo(bmi, bm.bmWidth, bm.bmHeight, bitsPerPixel);


    return true;

}

我咨询了 deleakers,发现我正在与 HDC 泄漏作斗争。我不确定我做错了什么。

最佳答案

你这里有资源泄漏:

hbmap = CreateCompatibleBitmap(GetDC(0), rc.right - rc.left, rc.bottom - rc.top);

你应该改成

HDC hdc = GetDC(0);
CreateCompatibleBitmap(hdc, rc.right - rc.left, rc.bottom - rc.top);
... 
ReleaseDC(0, hdc);//call this before exiting the function

free(pPixels) 是错误的(尽管在这种情况下它仍然可以清理)。将 free(pPixels) 替换为 delete[]pPixels

改变这个:

SelectObject(hdcShot, hBitmapOld);
DeleteDC(hdcShot);
DeleteObject(hbmapNew);
DeleteDC(hdcShotNew);

对此:

SelectObject(hdcShot, OldBmp);
DeleteObject(hbmapNew);
DeleteObject(hBitmapOld);
DeleteDC(hdcShot);
DeleteDC(hdcShotNew);

关于c++ - HDC内存泄漏(释放HDC/删除hdc),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38650459/

相关文章:

c++ - cin.clear() 如何清除输入缓冲区?

c - C中的字符串链表

android - LeakCanary 没有提供足够的信息来识别泄漏

objective-c - iOS:使用 self 导致内存泄漏?

c++ - 为什么在这种情况下 g++ 不遵守一项定义规则 (ODR)? 。

c++ - 是否有任何标准可以在 native C++ 中使用 Web 服务?

c++ - 使用 C/C++ 在 Windows 7(或任何平台,如果可能)上使用 Screenshot Taker

iphone - 消息发送到解除分配的实例......据我所知,没有理由应该解除分配

c - 释放图表时释放太多

wpf - 由于 “direct delegate roots”,System.Timers.Timer泄漏