1
votes

I've created a blank application with a mdi parent form opening a blank child form from the menu.

When the parent form of the child form is set to the mdi - it appears the system does not release memory - thus a leak.

When the parent form is not set, the child form is removed.

Does anyone know why this apparent memory leak can be resolved?

I've been using the ants memory profiler.

Bob.

3
Just to clarify this is a completely blank mdi.. the only code is as follows and is attached to the menu click: Dim frm As New Baby frm.MdiParent = Me frm.Show() So there are no event handlers added by myself.bob

3 Answers

1
votes

I think the most common source of memory leaks are events that are not properly released, but you said you looked for it.

It's hard to know exactly where the leak is without looking at the code. I would recommend you to use .NET Memory Profiler. I've used it before and it seems to be superior than Ants.

1
votes

This seems to be a bug in the .net framework 2.0 introduced with SP1 and still there in SP2.

Just read the following blog post SciTech software, makers of .net Memory Profiler:
http://www.scitech.se/blog/index.php/2008/03/20/minor-memory-leak-introduced-in-net-framework-20-sp1/

To fix the problem add the following code to your MDI parent form:

protected override void OnMdiChildActivate(EventArgs e)
{
  // Code from http://www.scitech.se/blog/index.php/2008/03/20/minor-memory-leak-introduced-in-net-framework-20-sp1/
  base.OnMdiChildActivate(e);
  try
  {
    typeof(Form).InvokeMember("FormerlyActiveMdiChild",
    BindingFlags.Instance | BindingFlags.SetProperty |
    BindingFlags.NonPublic, null,
    this, new object[] { null });
  }
  catch (Exception)
  {
    // Something went wrong. Maybe we don't have enough
    // permissions to perform this or the
    // "FormerlyActiveMdiChild" property no longer
    // exists.
  }
}

I tested this with ANTS Memory Profiler and it fixed the issue in my case.

0
votes

One possibility is if you are subscribed to events on an object, it will not be garbage collected. You must make sure nothing is referencing that object.