IDisposable and 'using' - GC giving me trouble...

  • Thread starter Thread starter Per Rollvang
  • Start date Start date
P

Per Rollvang

Hi all!

I have created a few classes that inherits the IDisposable-interface, and if
I don't use them in this fashion

using(bla bla bla)
{
// do stuff
}

the application seem to raise a 'non-fixable' exception at exit.

Is implementing IDisposable together with 'using' a preferred way to fire
the Dispose method, and not letting the GC handle things)?

I implement IDisposable to have an option to 'clean up' cleaning up exactly
when I want it too happen. Is this correct use of IDisposable?

TIA
Per Rollvang
 
It's almost always better to call Dispose explicitly rather than letting the
GC handle things.

The GC is only really concerned with memory - it doesn't have a good concept
of the costs of any other kind of resource. The upshot of this is that if
you rely on the GC to tidy up other kinds of resources, it often doesn't do
the cleanup as often as you would like.

This is why IDisposable was invented in the first place.
 
Back
Top