471,582 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,582 software developers and data experts.

Static Fonts and other Unmanaged Resources - where to dispose?

nij
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
Feb 16 '06 #1
2 1695
> ... 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.

Feb 16 '06 #2
nij
"AMercer" wrote:
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!

Feb 17 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Barry Anderberg | last post: by
reply views Thread by Fernando Cacciola | last post: by
4 posts views Thread by Rachel Suddeth | last post: by
4 posts views Thread by PromisedOyster | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.