Reference Question

bri189a

Senior Contributor
Joined
Sep 11, 2003
Messages
1,004
Location
VA
So I was reading that you should make sure to dispose any DataSet or DataView object on the Page_Unload event since they tie up resources and they won't be disposed otherwise - it will have to wait until GC gets around to Finalizing the object.

Simple enough - makes since until:

If you keep the DataView or DataSet in Session so you don't have to go through your data access classes and hit the database each time, you now have a potential problem. When you put a DataView or DataSet in Session it's put by reference (it's an object). So if you have:

In the fields:
WithEvents ds as DataSet

In a function somewhere:
SomeDataAdapter.Fills(ds)
Session("Whatever") = ds
ds.Tables.Clear()

And in a later function:

Dim dt as DataTable = DirectCast(Session("Whatever"), DataSet).Tables(0)

You will get a null reference.

So.....

If you dispose a DataSet or DataView that you have as an object on your page that you may set from Session or ViewState after initial load, and then in your Page_Unload event you're trying to Dispose that object, which because of the reference be trying to dispose an object in Session... which for one, can that even be done, and two, would that cause GC to re-register it for Finalize, and three if that occurs now it would live in Session until Session died because it's not in the fReachable queue (able to be finalized because it's not tied to any other objects).

Am I correct in all of this or am I way off?
 
The Dispose method found in DataSets and DataTables is actually due to it inheriting from Component, calling Dispose on a DataTable or DataSet doesn't actually free up the table(s) at all. Also neither of them provide their own finalizer so there is no need to worry about needing to re-register etc.
If you need the data to be available then store it in a Session, Application or Cache variable, when it is no longer required clear the Session, Application or Cache variable and let the GC collect it at it's leisure.
 
Back
Top