|
-
Sep 22nd, 2005, 07:37 PM
#1
Thread Starter
Addicted Member
Garbage Collection Not working!
I found this out while trying to figure something out.
When you try to use a data reader in a function:
Function LoadtoList()
Dim myLoad As New OleDbCommand("Select * from Questions order by Num",mycon)
dim rd as system.Data.OleDb.OleDbDataReader
myload.CommandType = myLoad.CommandType.Text
rd = myload.ExecuteReader()
lvqs.Items.clear()
While rd.Read()
Dim x As new ListViewItem
x.Text = rd.GetValue(0).ToString()
x.SubItems.add(rd.GetValue(1).ToString())
x.SubItems.add(rd.GetValue(2).ToString())
x.SubItems.add(rd.GetValue(3).ToString())
x.SubItems.add(rd.GetValue(4).ToString())
x.SubItems.add(rd.GetValue(6).ToString())
lvqs.Items.add(x)
End While
End Function
In here, when the function ended, the rd object must have been disposed because the the variable life cycle is just within the function. But it will not! try using any commands that will execute on the database and you will get an error that you there is an opened data reader.
-
Sep 22nd, 2005, 07:46 PM
#2
Re: Garbage Collection Not working!
You misunderstand how garbage collection works. When your variable loses scope the object it refers to BECOMES ELIGIBLE for garbage collection. This means that the system will clean up the object, but only when it is good and ready. You should still be calling Close and/or Dispose on any and all objects that support those methods when you are finished with them. Garbage collection makes your life easier but it does not mean you don't have to write proper code. It may be that there is another reference to that same object elsewhere so the system will not just destroy the object there and then, and if garbage collection was going on constantly it would degrade application performance. You should read up on the subject of garbage collection in the help system or on MSDN on-line rather than just making assumptions about how it works.
-
Sep 22nd, 2005, 07:50 PM
#3
Re: Garbage Collection Not working!
You must close the datareader after using it. It is just like a connection, you cannot open a connection when it is already open.
rd.Close()
-
Sep 22nd, 2005, 07:52 PM
#4
Thread Starter
Addicted Member
Re: Garbage Collection Not working!
im about the scope of the object. it is only on the function, not on the entire app
-
Sep 22nd, 2005, 07:56 PM
#5
Re: Garbage Collection Not working!
You need to differentiate between the variable and the object. The VARIABLE loses scope, but the variable is not the object. You can have any number of variables refer to the same object. The variables basically just contain the memory address of the object. Like I said, garbage collection does not abdicate you from your responsibilities as a programmer. When you are finished with an object, Close it if it supports that method and Dispose it if it supports that method.
-
Sep 22nd, 2005, 08:05 PM
#6
Thread Starter
Addicted Member
Re: Garbage Collection Not working!
-
Sep 22nd, 2005, 08:16 PM
#7
Re: Garbage Collection Not working!
An open DataReader blocks a Connection, so that's why you must close the object before using it again. If you try to a open a DataReader before closing the first one, you'll receive an exception saying "requires an open and available connection", simply because the connection already blocked by the first DataReader that's still open.
-
Sep 22nd, 2005, 08:18 PM
#8
Thread Starter
Addicted Member
Re: Garbage Collection Not working!
that should have been disposed.
-
Sep 22nd, 2005, 08:40 PM
#9
Re: Garbage Collection Not working!
I think you are comparing ADO and ADO.NET which is different. That's why mostly developers has experience with ADO might be surprised by this restriction.
Different Microsoft data access technologies have handled this scenario differently. If you try to open two firehose cursors against a SQL Server database using ADO, everything will just work and you won't receive an error. This is because the OLE DB specification states that when the current connection is blocked, the OLE DB provider will perform the requested action on a new connection. Microsoft data access technology has handled the scenario differently than its predecessor: VBSQL,RDO,ADO.NET raises error and DAO/Jet,ADO creates a new connection.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|