Should Dispose be called on DataTable and DataSet objects?

G

Guest

It is my understanding that the DataTable and DataSet objects are
"disonnected" (from the database) classes and support IMDB (In Memory
Database).

The IDisposable interface implemented in DataTable and DataSet is inherited
from the MarshalByValue interface which is there to support visual drag&drop
capabilities in Visual Studio.

We ran same test and after creating many big DataTable objects and
reclaiming the memory with GC.Collect() the DataTables are "disposed" and the
memory reclaimed and returned to the OS *REGARDLESS* of if we call
DataTable.Dispose or NOT.

Microsoft,

Do we need to call the Dispose methods for DataTable and DataSet objects and
if "Yes" - why?

Thank you in advance

Chavdar Angelov
 
M

Marina Levit [MVP]

Dispose is meant to clean up unmanaged resources before the object is
garbage collected. The garbage collector will call Dispose on each object it
is about to collect if that object implements the IDispose interface, to
give it one last chance to clean itself up.
Dispose itself does not force the object to be garbage collected.
Big difference.

AFAIK, right now, there are no unmanaged resources in those objects that
would need to be cleaned up via Dispose. In theory, in future versions of
the framework there may be.
 
S

Sericinus hunter

Marina said:
Dispose is meant to clean up unmanaged resources before the object is
garbage collected. The garbage collector will call Dispose on each object it
is about to collect if that object implements the IDispose interface, to
give it one last chance to clean itself up.

Can you please confirm? To my understanding, GC does not call Dispose,
but rather calls Finalize.
 
G

Guest

You said it in your first statement - "Dispose is meant to clean up unmanaged
resources....."

Therefore:

1. Any class implementing IDisposable has unmanaged resources.
2. Any class using any IDisposable classes is responsible for calling the
Dispose method on them.

If the DataTable and DataSet classes do not have any unmanaged resources in
them, why do they implement the IDisposable interface?

Is this just bad design from Microsoft or are we missing something here?

Thanks

Chavdar
 
S

Sericinus hunter

I think there is a misunderstanding here. First of all, the garbage
collector doesn't call Dispose, it calls Finalize, and it is your
responsibility to put a call to Dispose (if you have it) in a class
destructor. Then, you may want to implement IDisposable to clean up
at any time, not waiting for GC. And this clean up is not necessarily
related to unmanaged resources.
I've already asked Marina to explain in more detail but did not see
a response.
 
C

Cor Ligthert [MVP]

Sericinus.

Simple said.

The dispose in most classes is to make it your able to override it and with
that to make it you possible to finalize unmanaged resources. The dispose
is probably in 80% of the mostly used classes. That does not mean that you
*should* use it. (It is in 20% of the actual classes).

As Marina wrote, AFAWK the system.data namespace (ADONET) does not contain
unmanaged resources. Therefore there is no need to dispose any of those as
long as you have not added yourself something special to it.

Therefore if you are not sure if you should use the dispose and your class
implements the component model and with that Idisposable (by instance every
form), than don't use it. Some classes want it. That are by instance the
modal dialog form. Pens and attributes like that (more meant for the handles
not for the memory), hugh memory eaters as bitmaps.

There are by the active persons in this newsgroup, those who dispose every
label they use in their programs, just because that the label class has a
method dispose.

They tell, the dispose is not for noting. My opinion is that you should know
by every method why you use it.

I hope this helps,

Cor
 
S

Sericinus hunter

Thank you for your response, Cor.
Yes, I understand that you are not obliged to call Dispose. My point
is (maybe wrong though) that GS does not call Dispose, contrary to what
Marina said. This means that if you override existing Dispose or implement
it new and put some clean up code in there which you want to be executed,
then you should add call to Dispose in the destructor. This way you make
sure your clean up code will be executed even if you forget to call Dispose
by yourself, like with using{}. Once in the destructor, it will then be
called during the garbage collection process.
 

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