473,766 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1805
> ... 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
6600
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!
5
2108
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 problems I am employing the aforementioned software in trying to track down the problems. After an hour or so of program execution, a snapshot of the managed heap shows 27,025 BinaryReader objects as having been garbage collected but never...
6
1801
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 out ? best, eric
0
1214
by: Fernando Cacciola | last post by:
Hi People, Consider the following: class A : IDisposable { public A ( Stream aResource ) { mResource = aResource ; } public void Dispose() {
4
40030
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 deals with managed in one place, and code that deals with unmanaged in another place, but I can't seem to find anything that clearly explains what that means. I think if I used a Windows API function to optain a handle, that handle would be an...
9
2663
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 operations on purchase orders and inventory. I have created a PurchaseOrder class and Inventory class to encapsulate operations like creating POs, finding items, etc. These two classes are used extensively from different parts of the app.
4
2362
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 and Bitmaps that are loaded onto the form dynamically, eg control.Icon = ;. Should we explicitly Dispose of these unmanaged resources when the form closes OR should we leave it up to the garbage collector to remove these (and will it definitively...
3
3301
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 code snippet where it's overloading the Dispose method it disposes of managed and unmanaged resources if disposing == true, and only unmanaged resources if disposing is false. I'm not sure I completely understand. Thank you in advance.
4
4531
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 child apps of an IIS application and can be used by multiple users during the application life cycle and for multiple page loads for the same or different page under a root application. What I don't understand and need to know is whether that...
0
9571
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8835
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.