簡體   English   中英

HDC內存泄漏(發布HDC /刪除hdc)

[英]HDC memory leak (release HDC/ delete hdc)

我遇到這個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;

}

我檢查了刪除器,發現我正在努力解決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)是錯誤的(盡管在這種情況下仍然可以清理)。 delete[]pPixels替換free(pPixels)

改變這個:

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

對此:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM