Abuse of GDI+ resources

G

Guest

Recently some friends were looking at some code written by an outside
contractor. In the code, there was a great deal of use of pens and brushes
without disposing of them.

What surprised me is that the system never seemed to run out. The brushes
and pens were being created in every paint event. I suppose it's possible
that the garbage collector was getting around to them often enough to
finalize the pens and brushes, but I doubt it was happening often enough to
keep up with the use.

I've always thought of pens and brushes as very limited system resources. Is
that no longer the case with Gdi+?

Also, in a similar vein, we were discussing the use of a static pen object
to optimize drawing in a control.

Is this considered acceptable? I mean, the pen (and I presume its underlying
GDI+ handle) are going to be held for the lifetime of the app if it's
static.

Can someone help modernize my understanding of GDI?

Pete
 
A

AlexS

Hi, pdavis68

I think you just did not test enough or paint events were not that frequent
to demonstrate clearly that this is not the way to code.
While GC is able to collect some such references, depending on available
memory and some application characteristics, in some cases it will relocate
and keep in heap such objects. You can test how many resources are available
on particular system and how GC behaves by creating simple test application
with loop for allocations.
Basic rule of thumb is: if object implements IDisposable Dispose should be
called explicitly as soon as object is not in use anymore. However, this
simple rule is also not good enough for such possibly frequent events as
Paint. Every allocation takes some resources, so if you have a flood of
allocations you might / will run into problems sooner or later.
You can certainly use static or just instance members to keep required
brushes and pens. The only drawback - you have to watch for system color
changes and for changes in whatever other resources you use to construct
such objects. Instance members are more preferable from my point of view -
create objects in constructor and destroy them in Dispose or destructor.

At the same time, to check that GC is able or not able to cope, you might
want to consider to profile application behavior and especially heap
allocations. You will be able to see also when Finalize methods are called
on objects.

Generally acceptability of any code is related to how application is used,
so you have to be careful with simple rules. They might apply and apply not
depending on circumstances.

HTH
Alex
 
S

Stu Smith

Recently some friends were looking at some code written by an outside
contractor. In the code, there was a great deal of use of pens and brushes
without disposing of them.

What surprised me is that the system never seemed to run out. The brushes
and pens were being created in every paint event. I suppose it's possible
that the garbage collector was getting around to them often enough to
finalize the pens and brushes, but I doubt it was happening often enough to
keep up with the use.

Were you running on Win2k / XP? Try the program on Win98 / Me, they had much
tighter restrictions on the number of handles available.

Have a look at:

http://msdn.microsoft.com/msdnmag/issues/03/01/GDILeaks/default.aspx

It can be a useful tool, if a little difficult to use.
 
B

Bob Powell [MVP]

This bad practice is partly the fault of Microsoft whos GDI+ demos have
regularly been peppered with such faults.

Many of them are not fatal as you've discovered but persistent use of pens
and brushes without disposing of them can cause performance problems.

The GDI+ FAQ has a couple of articles that deal with the use and
reclamation, or not, of GDI+ pens, brushes and fonts.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Image transition effects, snap-to-grid and Layered Windows are
all discussed in May's edition of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top