Results 1 to 5 of 5

Thread: Quick OOP question - object lifetime

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2011
    Posts
    75

    Quick OOP question - object lifetime

    I've written a SQL Query class so that I can get query results in as few lines as possible throughout my application.
    My Query class has a DataTable property (called Results) so I can access the returned rowset after the query is executed in the New(SQLString as String) method.
    I've given it a load of 'Implements IDisposable' boilerplate I found online - so that I can use 'Using.. End Using' and all seems well. It's working.

    Can I just ask for a quick confirmation ( I think I'm getting the hang of OOP, but want to make sure) ... given the following code...

    Code:
            Dim copyDt As New DataTable
            Using test1 As New SQLstuff.Query("SELECT * from MyTable")
                MsgBox(test1.RowCount & "  " & test1.Results.Columns(0).ColumnName)
                copyDt = test1.Results  ' both are DataTable object instances
            End Using
            MsgBox(copyDt.Rows.Count & "  " & copyDt.Columns(0).ColumnName)
    The two MsgBoxes give the same results.

    Q) Does copyDt end up referencing the same object instance in memory as the original test1.results, and it persists after the 'Using.. End Using' block, even when test1 no longer exists, purely because the GC knows that copyDt is also pointing to the same data, and the whole lot will be GCollected and cleared after copyDt falls out of scope?

    (Optional second Question - I've run a query within a FOR loop for thousands of times (impressively quick!) and in Task Manager the amount of memory used by VB stays around the same, is that a valid way to check for memory leaks?)

    Thanks

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Quick OOP question - object lifetime

    Quote Originally Posted by geek648 View Post
    Q) Does copyDt end up referencing the same object instance in memory as the original test1.results, and it persists after the 'Using.. End Using' block, even when test1 no longer exists, purely because the GC knows that copyDt is also pointing to the same data, and the whole lot will be GCollected and cleared after copyDt falls out of scope?
    Thanks
    Yes, copyDt is the exact same object as test1.results. Also, disposing of an object does not invoke the GC to clean it up. Dispose is actually provided as a way for you clean up unmanaged resources as the GC cannot clean them up. You must implement IDisposable if your class uses unmanaged resources otherwise you would have memory leaks in any app that uses your class.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Quick OOP question - object lifetime

    Watching the memory is not a totally reliable way to check for leaks (or leeks, if you are vegetarian). The fact that the memory usage isn't going up is certainly a good sign, but if the memory WAS going up, that wouldn't necessarily mean that you are leaking memory. The app is liable to request more memory from the OS before it attempts to clean up what it has, and if the OS has plenty of memory, it is liable to comply with the request without forcing the app to clean house. Therefore, an increase in memory could just be an indication that the two (the app and the OS) have opted for the cheaper route of adding memory rather than the more expensive route of recovering fragmented memory blocks already allocated.

    So: Constant memory = Good, but Increasing memory does not = Bad....in all cases.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2011
    Posts
    75

    Re: Quick OOP question - object lifetime

    Thanks, it all seems to be behaving itself. Just 4 lines of code (including the End Using) gets me a SQL query result loaded into a datagridview, so I'm happy with my first real class

    My query method uses SqlConnection, SqlCommand and SqlAdaptor in 3 nested Usings, so I'm hopeful that everything is being cleaned up and won't come back to haunt me farther down the line!

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Quick OOP question - object lifetime

    It should be fine. I do pretty much the same thing in many places.
    My usual boring signature: Nothing

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