473,378 Members | 1,447 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,378 software developers and data experts.

Should objects that implement IDisposable always be Dispose'd?

[Please do not mail me a copy of your followup]

I notice that lots of the GDI+ objects implement IDisposable:
SolidBrush, FontFamily, Font, etc.

If I don't explicitly call Dispose() on these guys after I'm done with
them, what is the downside? Is it just a longer/more frequent garbage
collection, or is it worse?

Personally, I allocate such temporary objects inside C#'s using
statement, but not everyone does this and I'm wondering what the
repurcussions could be.
--
"The Direct3D Graphics Pipeline"-- code samples, sample chapter, FAQ:
<http://www.xmission.com/~legalize/book/>
Pilgrimage: Utah's annual demoparty
<http://pilgrimage.scene.org>
Jul 21 '05 #1
3 2792
"Rich" <le*************@mail.xmission.com> wrote in message
news:O7*************@TK2MSFTNGP10.phx.gbl...
I notice that lots of the GDI+ objects implement IDisposable:
SolidBrush, FontFamily, Font, etc.

If I don't explicitly call Dispose() on these guys after I'm done with
them, what is the downside? Is it just a longer/more frequent garbage
collection, or is it worse?

Personally, I allocate such temporary objects inside C#'s using
statement, but not everyone does this and I'm wondering what the
repurcussions could be.


These days I think it wold be next to impossible to inadvertantly exhaust
GDI resources. Nevertheless, I think it is good practice to Dispose()
objects if they supply Dispose(). Since .NET designers didn't build in a
deterministic destructor mechanism, the onus is on you as the consumer of
the object to play by the rules.

using() works well when the life cycle of the object can be localized within
a single routine.

-- Alan
Jul 21 '05 #2
If you are creating instances of these objects over and over (like whenever
you paint), calling Dispose() is a good idea.

If you only create one in your program, calling Dispose() before you exit is
okay, but not really required.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Rich" <le*************@mail.xmission.com> wrote in message
news:O7*************@TK2MSFTNGP10.phx.gbl...
[Please do not mail me a copy of your followup]

I notice that lots of the GDI+ objects implement IDisposable:
SolidBrush, FontFamily, Font, etc.

If I don't explicitly call Dispose() on these guys after I'm done with
them, what is the downside? Is it just a longer/more frequent garbage
collection, or is it worse?

Personally, I allocate such temporary objects inside C#'s using
statement, but not everyone does this and I'm wondering what the
repurcussions could be.
--
"The Direct3D Graphics Pipeline"-- code samples, sample chapter, FAQ:
<http://www.xmission.com/~legalize/book/>
Pilgrimage: Utah's annual demoparty
<http://pilgrimage.scene.org>

Jul 21 '05 #3
[Please do not mail me a copy of your followup]

"Eric Gunnerson [MS]" <er****@online.microsoft.com> spake the secret code
<ed**************@tk2msftngp13.phx.gbl> thusly:
If you are creating instances of these objects over and over (like whenever
you paint), calling Dispose() is a good idea.


Its not necessarily in response to a paint message, but it could be a
heavily travelled code path. Basically what I'm hearing confirms my
idea that the rule should be to call Dispose on anything that's
IDisposable.
--
"The Direct3D Graphics Pipeline"-- code samples, sample chapter, FAQ:
<http://www.xmission.com/~legalize/book/>
Pilgrimage: Utah's annual demoparty
<http://pilgrimage.scene.org>
Jul 21 '05 #4

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

Similar topics

10
by: Henrik Dahl | last post by:
Hello! After I've finished using an instance of the SqlCommand class, should I then invoke Dispose() on the instance. I suppose so, as there is a Dispose method, but what does it actually...
2
by: Billy Porter | last post by:
Greetings, I got a class that wraps the System.Data.SqlClient.SqlConnection class (no COM interaction). I'm not sure if I'm supposed to implement the IDisposable pattern for this wrapper or not....
7
by: Claire | last post by:
How do I make sure Ive disposed of my objects please? Ive added interface IDispose to a base class. Ive several sub classes inheriting from this base class. I store one or other of these...
3
by: Rich | last post by:
I notice that lots of the GDI+ objects implement IDisposable: SolidBrush, FontFamily, Font, etc. If I don't explicitly call Dispose() on these guys after I'm done with them, what is the...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.