This is .net WinForm question about MDI setting.
这是关于MDI设置的.net WinForm问题。
When the main form creates an MDI child form, the main form's PropertyStore
holds a reference to the MDI child form. I wonder whether this will cause the child form to be alive even if it is closed. If so, what shall I do when disposing the child form in order to remove this reference?
当主窗体创建MDI子窗体时,主窗体的PropertyStore包含对MDI子窗体的引用。我想知道这是否会导致儿童形态即使关闭也会存活。如果是这样,处理子表格时我该怎么办才能删除此引用?
The child form is called by sample code:
子表单由示例代码调用:
//The code is in the main form.
var f = new FormMDIChild();
f.MdiParent = this;
f.Show();
2 个解决方案
#1
For the record, the solution offered in the referenced post does work (though it's a little dicey). However, the leak also goes away if you open and close another child form, it appears that the MDI Parent only leaks the last opened child.
为了记录,参考文章中提供的解决方案确实有效(尽管它有点冒险)。但是,如果打开和关闭另一个子窗体,泄漏也会消失,看起来MDI Parent只会泄漏最后打开的子窗口。
If you want to fix the leak by using the work around mentioned in the referenced post, just override the MDIParent's OnMdiChildActivate method...
如果你想通过使用引用帖子中提到的工作来修复泄漏,只需覆盖MDIParent的OnMdiChildActivate方法......
protected override void OnMdiChildActivate(EventArgs e)
{
base.OnMdiChildActivate(e);
typeof(Form).InvokeMember("FormerlyActiveMdiChild",
BindingFlags.Instance | BindingFlags.SetProperty |
BindingFlags.NonPublic, null, this, new object[] { null });
}
#2
I'm having a similar problem. I found this forum post discussing the issue and suggesting a solution however I'm not sure if this property has been removed in recent service packs as I can't seem to find the property using reflection so the solution isn't working for me. I'll keep looking around and let you know if I find another solution.
我遇到了类似的问题。我发现这个论坛帖子讨论了这个问题并建议了一个解决方案但是我不确定这个属性是否已经在最近的服务包中删除了,因为我似乎无法使用反射找到该属性,所以解决方案对我不起作用。如果我找到另一个解决方案,我会继续环顾四周,让你知道。
#1
For the record, the solution offered in the referenced post does work (though it's a little dicey). However, the leak also goes away if you open and close another child form, it appears that the MDI Parent only leaks the last opened child.
为了记录,参考文章中提供的解决方案确实有效(尽管它有点冒险)。但是,如果打开和关闭另一个子窗体,泄漏也会消失,看起来MDI Parent只会泄漏最后打开的子窗口。
If you want to fix the leak by using the work around mentioned in the referenced post, just override the MDIParent's OnMdiChildActivate method...
如果你想通过使用引用帖子中提到的工作来修复泄漏,只需覆盖MDIParent的OnMdiChildActivate方法......
protected override void OnMdiChildActivate(EventArgs e)
{
base.OnMdiChildActivate(e);
typeof(Form).InvokeMember("FormerlyActiveMdiChild",
BindingFlags.Instance | BindingFlags.SetProperty |
BindingFlags.NonPublic, null, this, new object[] { null });
}
#2
I'm having a similar problem. I found this forum post discussing the issue and suggesting a solution however I'm not sure if this property has been removed in recent service packs as I can't seem to find the property using reflection so the solution isn't working for me. I'll keep looking around and let you know if I find another solution.
我遇到了类似的问题。我发现这个论坛帖子讨论了这个问题并建议了一个解决方案但是我不确定这个属性是否已经在最近的服务包中删除了,因为我似乎无法使用反射找到该属性,所以解决方案对我不起作用。如果我找到另一个解决方案,我会继续环顾四周,让你知道。