473,390 Members | 1,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Static Fonts and other Unmanaged Resources - where to dispose?


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?

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

Many thanks for taking the time to respond!

Feb 17 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Dolphin White | last post by:
For example, I allocate some unmanaged resources in the static constructors, then how can I properly release the resource before the application exit? thx!
by: Barry Anderberg | last post by:
I'm using a tool by Sci-Tech called the .NET Memory Profiler. We have a massive .NET/C# application here and it has been exhibiting memory leak behavior for some time. Attempting to remedy the...
by: Eric | last post by:
for example: SqlConnection is used in my project, how can I know if all connections were closed after open and execution. If some guys forget to close connections after using, how can i check it...
by: Fernando Cacciola | last post by:
Hi People, Consider the following: class A : IDisposable { public A ( Stream aResource ) { mResource = aResource ; } public void Dispose() {
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
by: Laban | last post by:
Hi, I find myself using static methods more than I probably should, so I am looking for some advice on a better approach. For example, I am writing an app that involves quite a bit of database...
by: PromisedOyster | last post by:
There are various contradictory newsgroup postings on this issue, but I would really like a definitive answer from the .NET gurus out there? We have various WinForms that contain multiple Icons...
by: DaTurk | last post by:
Hi, I'm implementing the Idisposable pattern and am wondering what the difference it between managed, and unmanaged resources? http://msdn2.microsoft.com/en-us/library/fs2xkftw.aspx in the...
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.