Why does the last MDI child form that was closed not get garbage collected?

最后都变了- 提交于 2019-12-04 12:35:31

Technically, because that Form is the "FormerlyActiveMdiChild". This looks like a bug. Fortunately, not a very serious one.

The ability to troubleshoot uncollected objects is a good skill to have. The windbg debugger from Microsoft that comes with the Debugging Tools for Windows (http://www.microsoft.com/whdc/devtools/debugging/default.mspx) is great for this purpose. In the walkthrough below, note that I have removed a lot of the output from windbg that is not pertinent.

  1. Instead of creating the MDI child instance of type Form, subclass it as TestChildForm to make it easy to identify.
  2. Start the executable and attach windbg. Load the .NET extensions with !loadby sos mscorwks.
  3. In windbg, run !dumpheap -type TestChildForm.

     Address       MT     Size
    01e2e960 001c650c      320  
    
  4. Next, run !gcroot 01e2e960.

    ESP:3de7fc:Root:01e29a78(System.EventHandler)->
    01e26504(WindowsFormsApplication1.Form1)->
    01e269b8(System.Windows.Forms.PropertyStore)->
    01e2ef04(System.Windows.Forms.PropertyStore+ObjectEntry[])
    
  5. Next, run !dumparray -details 01e2ef04 and search the output for 01e2e960.

          MT    Field   Offset                 Type VT     Attr    Value Name
    6797ea24  40032a3       10         System.Int16  1 instance       56 Key
    6797ea24  40032a4       12         System.Int16  1 instance        1 Mask
    6798061c  40032a5        0        System.Object  0 instance 01e2e960 Value1
    
  6. Finally, I ran !name2ee System.Windows.Forms.dll System.Windows.Forms.Form followed by !dumpclass 6604cb84 (as determined by !name2ee) and looked for 56.

          MT    Field   Offset                 Type VT     Attr    Value Name
    67982c4c  4001e80      fd8         System.Int32  1   static       56 PropFormerlyActiveMdiChild
    

If you would rather use the Visual Studio debugger instead of windbg, you must first enable Properties, Debug, Enable unmanaged code debugging. Substitute .load sos for .loadby sos mscorwks.

Chris

The reason why this is happening is quite simple there is still a reference to this form. The good news is that we can remove this reference.

Add an eventhandler for the form closing event.

private void ctlOpenMDI_Click(object sender, EventArgs e)
{
    Form newForm = new Form();
    newForm.FormClosing += new FormClosingEventHandler(form_Closing);
    newForm.MdiParent = this;
    newForm.Tag = new TestObject();
    newForm.Show();
}

And a method to handle the event.

private void form_Closing(object sender, EventArgs e)
{
    Form form = sender as Form;
    form.MdiParent = null;
}

Here we reset the MdiParent property, by doing this the form is removed from the MdiChild list of the parent. Now when the form is closed this reference will reset as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!