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

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
 
I

Ian Griffiths [C# MVP]

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.
 
P

Per Rollvang

Thanks a lot Ian,

at least I was doing something right...

Regards, Per Rollvang
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top