我是否必须手动删除每个实例的所有事件处理程序?

发布于 2024-09-10 12:11:11 字数 347 浏览 2 评论 0原文

考虑这个类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的黑色迷你裙 2024-09-17 12:11:11

如果您的对象被标记为死亡,则意味着没有对它的引用,并且其内部引用现在来自死亡对象,从这一点来看并不重要。因此您不必手动删除处理程序。

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.

娜些时光,永不杰束 2024-09-17 12:11:11

在对对象的所有引用(包括所有事件处理程序)消失之前,该对象不会被垃圾收集,也不会被销毁,直到它被垃圾收集。您真正需要做的是删除同一类中的处理程序,一旦您完成处理程序,就会添加处理程序。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文