Click to See Complete Forum and Search --> : Help! Leaking GDI
lauralime
June 1st, 2006, 05:25 AM
Hi,
I have the following code in a test harness (using VC++ 6.0) on XP
(I have stripped it out of some other code that was leaking GDI objects)
HDC hDC = CreateDC(driver, printer, port, devmode)
if (hDC)
{
devmode.orientation = DMORIENT_LANDSCAPE;
ResetDC(hPr, devmode)
DeleteDC(hPr)
}
Watching task manager ...
CreateDC creates 2 gdi objects
ResetDC creates another 1
DeleteDC only deletes 1
Therefore the above leaks 2 objects!
I have thousands of documents that are using this as printed and eventually I run out of GDI objects.
Does anyone have any ideas?
thanks
Laura
Rich2189
June 1st, 2006, 10:17 AM
quick get a boat were going to drown!.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_0d43.asp
Do a quick check to see if the dc returned from resetDC is the same as Hpr. If they are different then there is one leak.
lauralime
June 1st, 2006, 11:17 AM
No the handles are the same I checked that previously
but it still doesnt explain why ResetDC leaks a GDI object in Task Manager,
and even without calling ResetDC, just by calling Create then Delete a GDI object is still leaked by looking in Task Manager!
olivthill
June 2nd, 2006, 10:26 AM
Maybe it is because some objects (a pen, a brush, a bitmap, ...) have not been selected out. In the helpfile, I can read the following warning.
Before calling ResetDC, the application must ensure that all objects (other than stock objects) that had been selected into the device context have been selected out.
EDIT : Sorry, maybe I have not carefully read the question. Is the code you show the actual code, or is it a simplified example?
_UE_
June 6th, 2006, 12:42 PM
I don't know could it help or not... but currently I use Deleaker ( http://www.deleaker.com/ ) in my VC++ projects, it shows created GDI objects and you can see where each object was created. It helps to find what objects weren't released :)
lauralime
September 14th, 2006, 06:29 AM
Hi
I think its more obvious than that - if you knock up a quick MFC test harness and put in the following lines...
HDC hDC = CreateDC("WINSPOOL", "\\\\TIG-PRN0\\PRN13", "", NULL);
BOOL bDel = DeleteDC(hDC);
(obviously replacing that printer with a network or local printer of your own)
you should see that after the create in task manager, 2 GDI objects have been created.
When DeleteDC is called only 1 of them has been deleted.
So it looks like 1 GDI object is leaking for each call.
If you add the ResetDC in another one is leaked also!
HELP! Im sure this never happened with the older OS's.
Thanks
Laura
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.