事件处理和内存泄漏 [英] Event handler and memory leaks

查看:228
本文介绍了事件处理和内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我分析VB.NET的项目,有一些对象(孩子 MDI 形式)布置,但不被 GC 的除去。

I analyze a VB.NET project and there are some objects (child MDI form) that are disposed, but not removed by the GC.

在MemoryProfiler分析发现,除其他外,如下:

The MemoryProfiler analysis find, among others, the following:

这个实例配置,仍然   间接扎根由事件处理程序。   这通常表明   事件处理程序还没有被正确   除去是的常见原因   内存泄漏。下面的实例   直接植根由事件处理程序(S)。   调查他们获得更多的   有关此问题的信息。

"This instance is disposed and still indirectly rooted by an EventHandler. This often indicates that the EventHandler has not been properly removed and is a common cause of memory leaks. The instances below are directly rooted by EventHandler(s). Investigate them to get more information about this issue..."

现在,我试图找出应该这意味着,以及如何解决它。

Now, I try to figure out what should this mean and how to fix it.

我有一个MDI窗体和子窗体。子窗体不会被GC后收集了开启/关闭,显然是因为保持静止(间接?)的mdi窗体引用 EventHandlerList ...

I have a MDI form and a child form. The child form is not collected by the GC after a open/close, apparently because remains still (indirectly?) referenced by the MDIForm EventHandlerList...

这里是什么地方?如何解决?

What can it be and how do I to fix it?

我想建议在<一个修复href="http://stackoverflow.com/questions/1117983/about-propertystore-and-mdi-child-form/1533968#1533968">this螺纹,因为曾与在 PropertyStore ,现在这消除了MDI引用问题,但是出现了MDI EventHandlerList 引用子窗体...

I tried the fix recommended in this thread, because had a problem with the MDI reference in the PropertyStore, now this eliminated, but appeared the MDI EventHandlerList reference to the child form...

在一些code分析,我发现了一些

After some code analysis I observed some

AddHandler newMenu.Click, AddressOf ClickMenu

没有preceding与 RemoveHandler newMenu.Click,AddressOf ClickMenu 。难道是主要的原因是什么?

without preceding with RemoveHandler newMenu.Click, AddressOf ClickMenu. Could it be the main cause?

和,一个propos,是手柄

And, a propos, is the Handles

Private Sub ClickMenu(sender as Object, e as EventArgs) Handles newMenu.Click

好了

RemoveHandler newMenu.Click, AddressOf ClickMenu
AddHandler newMenu.Click, AddressOf ClickMenu

从视图中的内存分配点?

from the memory allocation point of view?

推荐答案

EventHandlerList 所使用的类提供了大量的活动,以更有效地处理内存仅分配空间是必要的,而不是在事件被宣布时,它的事件支持字段。因此,所有的活动为主要形式存储在那里。

The EventHandlerList is used by classes that provide large numbers of events to more efficiently handle memory by only allocating space for the event backing field when it is needed rather than when the event is declared. All events for the main form are therefore stored there.

我希望孩子形式监视,当其母公司可以关闭(这是一种常见的用例)并没有从的FormClosing 或<$退订C $ C>当收到该事件FormClosed 事件。然而,这只是一个猜测。为了确认,看看你的孩子的形式实现,并找到它预订来自父窗体事件的所有情况,然后确保有从这些事件中相应的退订。

I would expect the child form was monitoring when its parent may close (this is a common use-case) and it didn't unsubscribe from the FormClosing or FormClosed event when it received that event. However, that's just a guess. To confirm, look at your child form implementation and find all cases where it subscribes to events from the parent form, then make sure there is a corresponding unsubscription from those events.

更新
你发现菜单处理程序未删除很可能是你看问题的根源。尝试添加删除呼叫,看看是否能解决了泄漏。

Update
The menu handler you found that was not removed could well be the root of the issue you see. Try adding the remove call and see if that addresses the leak.

这篇关于事件处理和内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆