Search code examples
.netlanguage-agnosticevent-handlingdisposing

Do I have to manually remove all event handlers for each instance?


Consider this class:

Class Item : Inherits ItemBase

    Public Sub New
        AddHandler MyEvent, AddressOf MyEventHandler
    End Sub

    Private Sub MyEventHandler()    
    End Sub

    Private Sub MySecondEventHandler() Handles MyBase.MyEvent
    End Sub

End Class

Do I have to manually remove the handlers on destruction of this item?? isn't this done by the GC or other tool of the managed-code compiler?


Solution

  • If your object is marked as dead that means there are no references to it and its internal references are now from dead object and do not matter from this point. So you don't have to remove the handlers manually.