Should objects that implement IDisposable always be Dispose'd?

  • Thread starter Thread starter Rich
  • Start date Start date
R

Rich

[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.
 
Rich said:
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
 
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.
 
[Please do not mail me a copy of your followup]

"Eric Gunnerson [MS]" <[email protected]> spake the secret code
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.
 
Back
Top