我是否必须手动删除每个实例的所有事件处理程序?
考虑这个类:
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
我是否必须在销毁该项目时手动删除处理程序?这不是由托管代码编译器的 GC 或其他工具完成的吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的对象被标记为死亡,则意味着没有对它的引用,并且其内部引用现在来自死亡对象,从这一点来看并不重要。因此您不必手动删除处理程序。
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.