首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HDC内存泄漏(释放HDC/删除hdc)

HDC内存泄漏(释放HDC/删除hdc)
EN

Stack Overflow用户
提问于 2016-07-29 12:21:37
回答 1查看 1.5K关注 0票数 3

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

谢谢!

代码语言:javascript
复制
BITMAP bm;
HBITMAP hbmap;
HBITMAP hBitmapOld;
BITMAPINFO bmi;
HDC hdcShot;

..。

代码语言:javascript
复制
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函数(不是很重要,但它显示了一些变量是如何初始化的)

代码语言:javascript
复制
    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泄漏作斗争。我不确定我做错了什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-29 15:57:49

这里有一个资源泄漏:

代码语言:javascript
复制
hbmap = CreateCompatibleBitmap(GetDC(0), rc.right - rc.left, rc.bottom - rc.top);

您应该更改为

代码语言:javascript
复制
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)

更改此设置:

代码语言:javascript
复制
SelectObject(hdcShot, hBitmapOld);
DeleteDC(hdcShot);
DeleteObject(hbmapNew);
DeleteDC(hdcShotNew);

要这样做:

代码语言:javascript
复制
SelectObject(hdcShot, OldBmp);
DeleteObject(hbmapNew);
DeleteObject(hBitmapOld);
DeleteDC(hdcShot);
DeleteDC(hdcShotNew);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38650459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档