CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2008
    Posts
    15

    Leaking GDI resources

    Hi all:

    I have an application who uses some BS_OWNERDRAW style buttons, and in the dialog-procedure, some like this to draw them:

    case WM_DRAWITEM:
    {
    LPDRAWITEMSTRUCT pdis = (LPDRAWITEMSTRUCT) lParam;
    HICON hIcon = (HICON)LoadImage (mhok.hInstance, MAKEINTRESOURCE(idButton), IMAGE_ICON, 0,0, LR_DEFAULTCOLOR|LR_LOADTRANSPARENT);

    DrawIconEx (pdis->hDC, 0, 0, hIcon, 0, 0, 0, NULL, DI_NORMAL);

    // The doc. states: If an application processes this message, it should return TRUE.
    return TRUE;
    }

    The problem is that after running a while, the application behaves badly.

    After diggin a bit in Google -in the Forger's Win32 API Programming Tutorial- I have found: "If your program runs fine for a few minutes and then starts drawing strangely or not at all, it's a good sign that you're leaking GDI resources".

    Besides that, referring to the WM_DRAWITEM message, the MS doc state that: "Before returning from processing this message, an application should ensure that the device context identified by the hDC member of the DRAWITEMSTRUCT structure is in the default state".

    The question is: How can I ensure that the pdis->hDC member is in the default state?

    May be including

    RestoreDC (pdis->hDC, -1);

    before the return?

    Or perhaps

    ReleaseDC(pdis->hwndItem, pdis->hDC);

    in that place?

    Any suggestion would be appreciated.

  2. #2
    Join Date
    Jul 2008
    Posts
    15

    Re: Leaking GDI resources

    Not to say that I'm using MS VC++ and the plain vanilla API.

  3. #3
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Leaking GDI resources

    RestoreDC() can be used, but it must correspond to a SaveDC() call at the beginning of your paint routine. It would help to see the code for your paint routine as a leaking GDI resource should be readily apparent.

  4. #4
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Leaking GDI resources

    Quote Originally Posted by oldnewbie View Post
    Any suggestion would be appreciated.
    If you read about LoadImage Function in MSDN, you'd see:
    Remarks
    When you are finished using a bitmap, cursor, or icon you loaded without specifying the LR_SHARED flag, you can release its associated memory by calling one of the functions in the following table.

    Resource Release function
    Bitmap DeleteObject
    Cursor DestroyCursor
    Icon DestroyIcon
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  5. #5
    Join Date
    Jul 2008
    Posts
    15

    Re: Leaking GDI resources

    Quote Originally Posted by hoxsiew View Post
    RestoreDC() can be used, but it must correspond to a SaveDC() call at the beginning of your paint routine. It would help to see the code for your paint routine as a leaking GDI resource should be readily apparent.
    hoxsiev:

    Because my WM_PAINT message treatement include several routines -basically to move windows- and prior to any further investigation, do you suggest that the memory leak is in the process of this former message more that in the WM_DRAWITEM as I was suspecting?

    Thank for your input.

  6. #6
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Leaking GDI resources

    Could be either. Although, I don't know why you're moving windows from within a WM_PAINT handler. That sounds odd.

  7. #7
    Join Date
    Jul 2008
    Posts
    15

    Re: Leaking GDI resources

    Quote Originally Posted by VladimirF View Post
    If you read about LoadImage Function in MSDN, you'd see:
    VladimirF:

    I just add the LR_SHARED condition to my LoadImage functions, and although I've made just a quick pair of tests, the good old Task Manager suggest that you shot directly in the bull's-eye.

    Thank you very muuuuch for your quick response :-)

  8. #8
    Join Date
    Jul 2008
    Posts
    15

    Re: Leaking GDI resources

    Quote Originally Posted by hoxsiew View Post
    Could be either. Although, I don't know why you're moving windows from within a WM_PAINT handler. That sounds odd.
    hoxsiev:

    Indeed it is. It's just the result of a newbie design and probably a poor solution to scroll the content of a dialog box who can grow and shrink in size -i.e can include a variable number of elements, without the use of the standard scroll bars. Yes I know, it sounds brutal but do the work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured