HDC memory leak (release HDC/ delete hdc)

user3518291

I'm having trouble with this HDC memory leak. Could you guys check if I'm releasing/deleting HDC correctly?

Thank you!

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 (not really important but it shows how some variables are initialized)

    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;

}

I checked with deleakers and found out that I'm struggling with HDC leak. I'm not sure what I've done wrong.

Barmak Shemirani

You have a resource leak here:

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

You should change to

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) is wrong (although it still cleans up in this case). Replace free(pPixels) with delete[]pPixels

Change this:

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

To this:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Scrapy hidden memory leak

From Dev

Concurrent query and delete, leads to Memory Leak in Neo4j 2.0.3 community

From Dev

Error C2360: Initialization of 'hdc' is skipped by 'case' label

From Dev

Owner-drawn button, WM_CTLCOLORBTN and WM_DRAWITEM (clearing an HDC)

From Dev

SQLite no release memory after delete

From Dev

Where to define PAINTSTRUCT and why to define HDC?

From Dev

Why does Python 2.7 sign extend HDC returned from GetDC() on x64?

From Dev

Any memory leak by delete and create same object several times

From Dev

How to delete a 2D vector of object pointers without a memory leak? C++ and SFML

From Dev

javascript interval memory leak

From Dev

shared_ptr memory leak without delete operator

From Dev

Python memory leak: do I need to delete?

From Dev

Node.js: How to release Mongoose model from memory? (memory leak)

From Dev

Why no memory leak for: @property(copy) NSString* name, when I don't release it in dealloc?

From Dev

Memory Leak in Scrapy

From Dev

What happens to an HDC when its window is destroyed?

From Dev

Delete linked list, memory leak C++

From Dev

Does memory leak when invoking delete over parent pointer after reinterpret_cast-ing?

From Dev

OpenGL drawing to an existing HDC using Texture2D

From Dev

How to release memory in android to avoid memory leak

From Dev

How can this delete to prevent the memory leak?

From Dev

Is this a memory leak

From Dev

hrc = wglCreateContext(hdc) , wglMakeCurrent(NULL,NULL) and glEnd() throw INVALID_OPERATION

From Dev

Owner-drawn button, WM_CTLCOLORBTN and WM_DRAWITEM (clearing an HDC)

From Dev

Get pixel data as array from hdc

From Dev

what's the difference between /dev/hdc, /dev/sr0, /dev/cdrom

From Dev

SDL- get HDC device context for OpenGL/OpenCL shared context

From Dev

Is there a memory leak?

From Dev

Can I set one hRC to multiple hDC?

Related Related

  1. 1

    Scrapy hidden memory leak

  2. 2

    Concurrent query and delete, leads to Memory Leak in Neo4j 2.0.3 community

  3. 3

    Error C2360: Initialization of 'hdc' is skipped by 'case' label

  4. 4

    Owner-drawn button, WM_CTLCOLORBTN and WM_DRAWITEM (clearing an HDC)

  5. 5

    SQLite no release memory after delete

  6. 6

    Where to define PAINTSTRUCT and why to define HDC?

  7. 7

    Why does Python 2.7 sign extend HDC returned from GetDC() on x64?

  8. 8

    Any memory leak by delete and create same object several times

  9. 9

    How to delete a 2D vector of object pointers without a memory leak? C++ and SFML

  10. 10

    javascript interval memory leak

  11. 11

    shared_ptr memory leak without delete operator

  12. 12

    Python memory leak: do I need to delete?

  13. 13

    Node.js: How to release Mongoose model from memory? (memory leak)

  14. 14

    Why no memory leak for: @property(copy) NSString* name, when I don't release it in dealloc?

  15. 15

    Memory Leak in Scrapy

  16. 16

    What happens to an HDC when its window is destroyed?

  17. 17

    Delete linked list, memory leak C++

  18. 18

    Does memory leak when invoking delete over parent pointer after reinterpret_cast-ing?

  19. 19

    OpenGL drawing to an existing HDC using Texture2D

  20. 20

    How to release memory in android to avoid memory leak

  21. 21

    How can this delete to prevent the memory leak?

  22. 22

    Is this a memory leak

  23. 23

    hrc = wglCreateContext(hdc) , wglMakeCurrent(NULL,NULL) and glEnd() throw INVALID_OPERATION

  24. 24

    Owner-drawn button, WM_CTLCOLORBTN and WM_DRAWITEM (clearing an HDC)

  25. 25

    Get pixel data as array from hdc

  26. 26

    what's the difference between /dev/hdc, /dev/sr0, /dev/cdrom

  27. 27

    SDL- get HDC device context for OpenGL/OpenCL shared context

  28. 28

    Is there a memory leak?

  29. 29

    Can I set one hRC to multiple hDC?

HotTag

Archive