I have a question about garbage collection when an object seemingly falls out of scope but may still stay alive as other classes are still holding references to it. Please look through the code below. Thanks.
Public Class MainForm
Private Sub Work
Dim Obj1 as New DataTable
Dim Obj2 as New DataTable
Helper.TestMethod1(Obj1)
Helper.TestMethod2(Obj2)
End Sub
End Class
Public Class Helper
Private Shared Obj1Reference as Object
Public Shared Sub TestMethod1 (ByVal obj1Ref as Object)
Obj1Reference = obj1Ref
End Sub
Public Shared Sub TestMethod2 (ByVal obj2Ref as Object)
'Do Something with obj2Ref
End Sub
End Class
Would both Obj1 and Obj2 be put on the garbage collection queue after the Work method exits. From my understanding, Obj2 is put on the queue but not Obj1 as the static helper class is holding a reference to Obj1. Please correct me if I am wrong.
Nothing is collected because neither of your objects have been initialized and are therefore both
Nothing. If they were initialized, then yes, one would continue to exist because it would continue to have a reference to it and the other would not because it would not have a reference to it.