Link to home
Start Free TrialLog in
Avatar of mahtieubrault
mahtieubrault

asked on

GDI leak?

Hi,

I use this code under Windows 2000:

HBRUSH hBrushTest;
hBrushTest = CreateSolidBrush( RGB(0,0,0) );
DeleteObject( hBrushTest );

After the creation of the brush, the GDI count in the task manager (when you show that column) increases by one. Unfortunately, the count doesn't decrease after the deletion of the brush.

What's wrong with this picture???
Avatar of jhance
jhance

I don't think that proves a leak.  What if you do:

HBRUSH hBrushTest;

for(int i=0; i<1000; i++){
  hBrushTest = CreateSolidBrush( RGB(0,0,0) );
  DeleteObject( hBrushTest );
}

Does your resource count decrease by 1000?  I think not.
I wonder if Windows is being a bit clever here. The brush you're creating there is identical to what you'd get if you did a GetStockObject(BLACK_BRUSH), so maybe Windows is actually setting up and returning the default object?
Avatar of mahtieubrault

ASKER

By doing the loop 1000 times, it increases the GDI count by 10. And it won't be cleared until the application is closed.  If it's in a dialog that's going to be used only once, I don't want the resources to stay in memory after using the dialog.

Any idea?
The good news is however, never the mind how many times you are running this loop (I did it until 30.000) the GDI handle count just climbs up by 10. But you are right, it is quite strange.
>>I don't want the resources to stay in memory after using the dialog.

Then don't allocate them.  You DO NOT have direct control over how or when Windows manages its resources.  As long as you "clean up" every object you create, you won't leak terminally.  Windows caches object store in the assumption that it will likely be reused before the app exits.

If you don't want ANY resources used, then don't allocate any, but, of course, that generally results in a useless application.
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial