Posted By: mohit.chawla | Feb 1st, 2006 @ 4:44 AM
page 1 of 1
Comments: 3 | Views: 3795
Lately i have come accross a very strange behaviour of the dispose
method. But before that can anyone tell me the difference between the
Dispose method and calling Nothing. Can one replace the other.
Now the problem:
I created a datatable and filled it with some rows using dataadapter.
then i accessed the rows, which worked fine. But even if you call the
dispose method of the datatable, and then try to access the rows in it.
You will be amazed that it will still show the rows. This behaviour is
very strange and now has put me in a state of confusion.
Dispose tells the object to release any resources it might be using. This is necessary to ensure that files are closed, etc. In this particular case, DataTable inherits Dispose from its base class, MarshalByValueComponent. The inherited implementation disconnects the DataTable from any component container it might be in (e.g. if you drop a DataTable into the Components tray of the Windows Forms designer).

Because of this, the data that's actually stored in the DataTable is still available after you call Dispose. You can't rely on this in general though - you should assume that the object will no longer work correctly after you've called Dispose.

In general if an object you're using has a Dispose method or implements the IDisposable interface, you should ensure you call it when you're done with the object. Otherwise, if cleanup is critical, the object will end up on the finalization queue, which is processed by one single, separate thread. This thread can end up being a bottleneck. The object will also survive longer than it needs to and so more memory will be consumed.

Setting any object reference to Nothing (or null in C#) tells the garbage collector that it can now process that object and, if no more references to the object exist, it can free the memory occupied by that object. (I'm trying to simplify here - actually the GC checks all object references and treats all objects no longer referenced as garbage.) It is only necessary to set references to Nothing if the reference has a longer lifetime than the object, for example if you have a Shared (static) variable or a variable in a Module and you want to free up an object. If an object reference is only used within a method, there's no need to set it to Nothing before returning.
Hi Mike,

Thank you for your reply, but another thing which has put me in confusion again is that the Connection class and the Command class also do not impliment the IDisposable and inherits from componentModel.Component. 

But on call of the dispose method of any of them, Dispose does what it is supposed to do, i.e. it closes the connection and releses the connection. 

So if dispose behaves this way for other classes then y not for datatable or a dataset.




mohit.chawla wrote:
Hi Mike,

Thank you for your reply, but another thing which has put me in confusion again is that the Connection class and the Command class also do not impliment the IDisposable and inherits from componentModel.Component. 

But on call of the dispose method of any of them, Dispose does what it is supposed to do, i.e. it closes the connection and releses the connection. 

So if dispose behaves this way for other classes then y not for datatable or a dataset.


Because they're coded that way.

Take a look at the documentation for IDisposable; it's simply

"a method to to release umanaged resources"

There's no tight coupling with object disposal.

So the reason why datatable and datasets can still be accessed is probably because the parts you are accessing aren't unmanaged resources, so dispose does nothing to touch those bits.

(In fact if you fire up reflector and look at DataTable it only has a dispose method because it's ineriting from MarshalByRefComponent and does not implement its own Dispose method. The documentation hints at this. So DataTable, and DataSet's dispose methods don't touch anything that is specific to those objects, hence the behaviour you're seeing.

Just remember that Dispose is detached from object finialisation/release, and can be called at any time.
page 1 of 1
Comments: 3 | Views: 3795