|
-
May 19th, 2010, 05:06 AM
#1
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.
-
May 19th, 2010, 05:14 AM
#2
Re: Leaking GDI resources
Not to say that I'm using MS VC++ and the plain vanilla API.
-
May 19th, 2010, 07:29 AM
#3
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.
-
May 19th, 2010, 10:33 AM
#4
Re: Leaking GDI resources
 Originally Posted by oldnewbie
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...
-
May 19th, 2010, 11:19 AM
#5
Re: Leaking GDI resources
 Originally Posted by hoxsiew
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.
-
May 19th, 2010, 11:30 AM
#6
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.
-
May 19th, 2010, 11:45 AM
#7
Re: Leaking GDI resources
 Originally Posted by VladimirF
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 :-)
-
May 19th, 2010, 11:57 AM
#8
Re: Leaking GDI resources
 Originally Posted by hoxsiew
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|