Static Fonts and other Unmanaged Resources - where to dispose?

G

Guest

Hi,

I am developing a C# control for my own use that will be used lots of times
on a form. It's inherited direct from Control, so I am doing all the drawing
myself... and as there are lots of possible 'bells and whistles' that each
control may use, there are lots of brushes, pens, fonts etc that I want to
optimise the use of. In other words, lots of unmanaged resources that need
to be Disposed after use.

The main optimisation that I wanted to do was to make these items STATIC.
This should be a great thing to do because then I have one initialisation of
each of these things, instead of potentially hundreds.

The trouble is, where would I then safely dispose of these items? The
'standard dispose mechanism' is well documented for instance variables, but
as far as I can tell there is no sure way to manage disposal of anything
declared static... or can I rely on .NET to correctly free such windows
resources when the program is closed?

Can anyone help?

Nij
 
G

Guest

... or can I rely on .NET to correctly free such windows
resources when the program is closed?

If the controls will exist until program termination, then I would not worry
about disposing anything, and your idea of using statics should be ok. I do
the same in a couple of forms of mine without any problems.
 
G

Guest

AMercer said:
If the controls will exist until program termination, then I would not worry
about disposing anything, and your idea of using statics should be ok. I do
the same in a couple of forms of mine without any problems.

Thanks AMercer,

So far, I have not had any (apparent) issues with resource leakage - like
from all the time I didn't understand IDisposable at all and was using 'new
Font' like crazy and not even within a 'using' block! So, like you, I am
reasonably confident that a little bit of abuse of the system here and there
won't muck things up for too many people.

But then I read much of "Effective C#" by Bill Wagner, and lots of other
stuff about correct usage of the 'Standard Dispose Pattern' like in MSDN
help... but nothing mentions unmanaged resources and static members, so...

But I'd still be keen to hear if there is some secret way that .Net, or
perhaps Windows itself, will detect e.g. my program termination, and
automatically clear up dangling resources at that point, or if this really is
a black hole in the C# spec, or just 'not good practice' (and why) or
whatever!

Many thanks for taking the time to respond!
 

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