How to properly dispose of used objects

CzechMan

New member
Joined
Feb 26, 2005
Messages
4
Hello,

I think my question is more or less about how to properly dispose of objects Ive used. Im coming over from ASP 3.0 where Ive learned to dispose of everything I dont need, so I do the same thing in VB.NET, though lately as my project grows I think Im fighting with the Garbage Collection.
I can click on a page where I access the DB and get an error like the one below (the exact error varies but most of the time its either one of those mentioned) then I can referesh the page and everything works just fine. So it leads me to believe that the GC is kicking in at times where Im dispousing of some objects (Readers, OleDbCommand, OleDbConnection etc.).
Whats the proper way of dispousing of ojects and is that whats causing my problems ? In most VB.NET examples people dont dispouse of objects so I guess I should do the same ? Thanks for your time.


Exception Details: System.InvalidOperationException: There is already an open DataReader associated with this Connection which must be closed first.
Source Error:
Line 65: MyCMD = New OleDb.OleDbCommand(SQL, MyConn)
Line 66: MyRead = MyCMD.ExecuteReader
> Line 67: MyCMD.Dispose()
Line 68: While Not MyRead.Read = False


Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 56: MyCMD = Nothing
Line 57: dRepeater.DataSource = DS
> Line 58: MyConn.Dispose()
Line 59: MyConn = Nothing
Line 60: dRepeater.DataBind()
 
You can only have one active DataReader on a connection at any given time, however there is nothing preventing you using more than one DataReader as long as you close each one before using the next.
I would generally keep the connection open till the last Reader in the routine has finished and then close it, I would close each Reader as soon as I have finished using it - just to prevent having multiple active ones by accident.
 
As was pointed out the your problem was not garbage collection. But I just have some notes about garbage collection that I would like to share.

If an object has a .Dispose method then by all means, dispose of it. It will free up any unmanaged resources. As far as garbage collection goes, I dont know if it is the VB compiler, MSIL compiler, or what exactly that does it, but something determines "safe points" where the garbage can be collected without causing you any worry or problems. Although some good practice on your part might help making memory management go more effiecently, you generally dont need to take any precautions when it comes to garbage collection .
 
Thanks Guys. I think Im getting the hang of it. Ive managed to get rid of the first problem by removing the MyCMD.Dispouse line. Now for the 2nd problem nothing I do seems to help :(

There is only one active DataReader at the time, and I do close all of them. The thing is that the error can show up on any page (that accesses the DB) no matter how coded, for example this morning I go the error on this page:

Code:
Call OPEN_DB()
Dim MyRead As OleDb.OleDbDataReader
Dim MyCMD As New OleDb.OleDbDataAdapter("sp_hc_NEWS_VIEW", MyConn)
MyCMD.SelectCommand.CommandType = CommandType.StoredProcedure
MyCMD.SelectCommand.Parameters.Add("@ID", OleDb.OleDbType.Integer).Value = sID
MyRead = MyCMD.SelectCommand.ExecuteReader()
If Not MyRead.Read = False Then
    TITLE = MyRead.GetString(0)
    ARTICLE = MyRead.GetString(1)
    sDATE = MyRead.GetDateTime(2).ToShortDateString
    ACTIVE = CONVERT_TXT(MyRead.GetInt32(3).ToString)
    ANAME = MyRead.GetString(4)
End If
MyRead.Close()
MyCMD.SelectCommand.Connection.Close()
Page.DataBind()

Any ideas what else could be wrong ?


The error was:
System.NullReferenceException: Object reference not set to an instance of an object.

Line 43: End If
Line 44: MyRead.Close()
> Line 45: MyConn.Dispose()
Line 46: MyConn = Nothing
Line 47: Page.DataBind()
 
PS:

I changed the
MyConn.Dispose()
MyConn = Nothing
for
MyCMD.SelectCommand.Connection.Close()
hoping it would help, but thats not really a solution.
 
Back
Top