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

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

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
Nov 16 '05 #1
2 1097
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.
--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/

"Per Rollvang" <perrollvang(at)hotmail.com> wrote:

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?

Nov 16 '05 #2
Thanks a lot Ian,

at least I was doing something right...

Regards, Per Rollvang

"Ian Griffiths [C# MVP]" <ia*************@nospam.nospam> wrote in message
news:OP****************@TK2MSFTNGP09.phx.gbl...
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.
--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/

"Per Rollvang" <perrollvang(at)hotmail.com> wrote:

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?


Nov 16 '05 #3

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

Similar topics

2
by: Bendik Engebretsen | last post by:
My main project is an MFC application. Its document class derives from COleServerDoc and I have implemented an Automation interface based on IDispatch. I am using VisualBasic to test the Automation...
9
by: Razzie | last post by:
Hey all, When should or should you not inherit from the IDisposable interface for your custom classes? Say I have a custom class SomeClass that does... some stuff. If I create a new instance...
2
by: Bendik Engebretsen | last post by:
My main project is an MFC application. Its document class derives from COleServerDoc and I have implemented an Automation interface based on IDispatch. I am using VisualBasic to test the Automation...
4
by: Helge Jensen | last post by:
In C# 2.0 System.IO.Stream is declared as: public class Stream: ..., IDisposable { ... public void Dispose(); public void Dispose(bool); IDisposable.Dispose(); } Which must be a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.