Solved

GDI leak?

Posted on 2002-07-11
8
632 Views
Last Modified: 2013-12-03
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???
0
Comment
Question by:mahtieubrault
8 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 7147943
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.
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 7148455
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?
0
 

Author Comment

by:mahtieubrault
ID: 7148910
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?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Expert Comment

by:charlass
ID: 7149077
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.
0
 
LVL 32

Expert Comment

by:jhance
ID: 7149319
>>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.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 10787458
PAQed - no points refunded (of 200)

modulo
Community Support Moderator
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now