Hi all , May i know should I set an object to nothing even after implementing an IDisposable method to my custom class ? Correct me if i am wrong , if you set it to nothing you are just setting the object to its default value . As you might risk losing a body of references which the GC cant be able to trace later on .

Class TestObject
   Implements IDisposable



    ' Implement IDisposable.
    Public Overloads Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub

   Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)

        If disposing Then
        End If
   End Sub


     Protected Overrides Sub Finalize()
        Dispose(False)
    End Sub


End Class
Dim x as new TestObject

'..processing 


x.Dispose()
x = Nothing

Recommended Answers

All 3 Replies

What happens if you test that theory on a String?

I assume you meant like the following

Dim x as string = "Test"

x = nothing

By doing the above , you are just setting the x to its default type value and removing the x (string)'s pointer to the data "test" null .

I want to know is it feasible to set custom objects with filled properties to null. Does setting it to nothing , assist GC in any way ? From what i understand , it only removes the references of the data and makes it harder to trace for the GC. Please correct me i am wrong in anyway . =)

Well, Nothing is the default value of a String, which is different than an empty string.

But I understand what you are asking.
From what I know, (after setting the object to a value) it's no better or worse than setting it to a new value.
It would create a new (Nothing) object and the old one is available for GC.

I would then imagine the bodiless reference to the new (null/Nothing) object is removed when it goes out of scope or no longer has any references.

So, it would not be better to set it to Nothing in order to "help" it be GC'd.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.