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 oth开发者_如何学Cer tool of the managed-code compiler?
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.
The object won't be Garbage Collected until all references to it have gone (including all event handlers), nor will it be destroyed until it is Garbage Collected. What you really need to do is remove the handlers in the same class which adds the handlers as soon as you've finished with them.
精彩评论