Results 1 to 9 of 9

Thread: Garbage Collection Not working!

  1. #1

    Thread Starter
    Addicted Member iehjsucker's Avatar
    Join Date
    Sep 2005
    Posts
    150

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    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()

  4. #4

    Thread Starter
    Addicted Member iehjsucker's Avatar
    Join Date
    Sep 2005
    Posts
    150

    Re: Garbage Collection Not working!

    im about the scope of the object. it is only on the function, not on the entire app

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Addicted Member iehjsucker's Avatar
    Join Date
    Sep 2005
    Posts
    150

    Re: Garbage Collection Not working!

    very bad...

  7. #7
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    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.

  8. #8

    Thread Starter
    Addicted Member iehjsucker's Avatar
    Join Date
    Sep 2005
    Posts
    150

    Re: Garbage Collection Not working!

    that should have been disposed.

  9. #9
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    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
  •  



Click Here to Expand Forum to Full Width