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?
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?