By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,915 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,915 IT Pros & Developers. It's quick & easy.

Should objects that implement IDisposable always be Dispose'd?

P: n/a
[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
Share this Question
Share on Google+
3 Replies


P: n/a
"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

P: n/a
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

P: n/a
[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 discussion thread is closed

Replies have been disabled for this discussion.