删除基本表单上的处理程序
我想知道是否可以在 Closing 方法中从一个基本表单中删除所有子表单中添加的所有事件处理程序。 (VB.NET;.NET 2.0)
背景: 在一个项目中我分析了一个内存问题。我用内存配置文件进行了验证(请参阅相关问题)并发现某些表单未收集由 GC 处理,可能是因为 EventHandler 未删除引用。
项目中所有表单都继承自BaseForm。现在我搜索在 OnClosed 方法中删除 foreach 控制所有往返于 ChildForms 的 EventHandler 的方法。
这可能吗?
I wonder if I can remove all added event handlers in all the child forms from the one base form in the Closing method. (VB.NET; .NET 2.0)
Background:
In a project I analyze a memory problem. I verified with the memory profile (see related question) and find out that some forms are not collected by the GC, probably because of the EventHandler non-removed references.
In the project all the forms inherits from the BaseForm. Now I search I way to remove in the OnClosed method foreach Control all the EventHandlers to/from the ChildForms.
Is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,Windows 窗体显然使这变得非常困难。它使用事件的添加和删除访问器,将事件处理程序委托存储在列表中。从该列表中删除处理程序的唯一有记录的方法是使用RemoveHandler,提供完全相同的AddressOf参数。从技术上讲,可以使用反射来查看列表,您需要首先找到用于标识列表中事件的秘密“cookie”。您需要使用 Reflector 或参考源来查找 cookie 的名称。
查看此论坛帖子,了解 MenuItem 导致泄漏的可能原因。
No, Windows Forms makes this explicitly very difficult to do. It uses the add and remove accessors of the event, storing the event handler delegate in a list. The only documented way to remove the handler from that list is to use RemoveHandler, providing the exact same AddressOf argument. Poking the list is technically possible with Reflection, you'll need to first find the secret "cookie" that's used to identify the event in the list. You'll need to use Reflector or the Reference Source to find the name of the cookie.
Review this forum post for a possible reason why MenuItem causes leaks.
好吧,您可以使用反射来查找所有事件。但我认为没有办法枚举分配给事件的处理程序。只有事件属性的添加和删除访问器。
Well, you could use Reflection to find all the events. But I think there's no way to enumerate the handlers assigned to an event. There're only add and remove accessors for event properties.