0

I have created a sample windows forms application which contains two forms - form1 and form2.

Form1 contains a button and on the click I am showing form2 as a dialogbox as given below.

private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            try
            {
                form2.ShowDialog();
            }
            catch (Exception ex)
            {

            }
            finally
            {
                if (form2 != null)
                {
                    form2.Dispose();
                    form2 = null;
                }
            }   

        }

Then i checked the application like Click on the button, then it will open form2, close it. and continued this for some 6 times.

When i checked the application with DevPartner, it is always showing Form2 form2 = new Form2(); as leaked

When i checked in the web it is saying that if we are using ShowDialog we need to dispose the form after we closing it,ie why i tried disposing in the finally block. But still it is showing that line as leaking. Can anyone please give your suggestions on this leak.

Mahesh KP
  • 6,248
  • 13
  • 50
  • 71
  • I always use a using block around my forms that use show dialog, have you tried this? – Sayse Aug 08 '13 at 07:19
  • I don't usually do that kind of style of disposing a form. Why not try putting that Dispose code in a button present in form2? – Jack Frost Aug 08 '13 at 07:19
  • @Sayse: yes initially i tried this using.. but no luck.. – Mahesh KP Aug 08 '13 at 07:21
  • @JackFrost: Dispose code in a button in form2.? sorry didn't get you? – Mahesh KP Aug 08 '13 at 07:22
  • Are you sure the leak doesn't go away after some time? it could just be that the gc hasn't got round to cleaning it up yet.. You could try adding `GC.Collect` but categorically **do not** keep this in your code – Sayse Aug 08 '13 at 07:24
  • @mahesh - say you create/have a button in you form2, there, place your "this.Dispose" code or place it in "FormClosing" either way you like. – Jack Frost Aug 08 '13 at 07:33
  • @JackFrost - That would potentially stop the form from closing safely, also if it was a button then you would have to require your user to click your button to fix your "bug" – Sayse Aug 08 '13 at 07:35
  • I know. I just said that for "Memory Leak Check" purposes only. – Jack Frost Aug 08 '13 at 07:39

3 Answers3

0

You should investigate your Form2 to find any subsctioptions to external events and rooted refferencies. All of them should be removed on form dispose.

In the following code the caues of the leak is a subscription to the Idle event. Subscription is removed in Dispose method to avoid a leak.

Form2()
{
    Application.Idle += Application_Idle;
}

void Application_Idle(object sender, EventArgs e)
{   
}   

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        Application.Idle -= Application_Idle;
    }
}
Viacheslav Smityukh
  • 5,652
  • 4
  • 24
  • 42
0

As a rule, when you use an IDisposable object, you should declare and instantiate it in a using statement. The using statement calls the Dispose method on the object in the correct way, and it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.

The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object.

Rohit
  • 10,056
  • 7
  • 50
  • 82
0

It is probably the checking rules of your tool, since the creation of the instance is not within the try-catch.

Try it like this:

private void button1_Click(object sender, EventArgs e) {
    Form2 form2;
    try {
        form2 = new Form2();
        form2.ShowDialog();
    } finally {
        if (form2 != null) {
            form2.Dispose();
            form2 = null;
        }
    }
}

Or even simpler:

private void button1_Click(object sender, EventArgs e) {
    using (var form2 = new Form2()) {
        form2.ShowDialog();
    }
}
Maarten
  • 22,527
  • 3
  • 47
  • 68
  • Is there any difference between my code and your first sample? Even if we instantiate form2 outside try block,i think then also it will be disposed in the final block. – Mahesh KP Aug 08 '13 at 08:27
  • True, but if your Form2 throws an exception in the constructor, then it will not be disposed. Which is what your tool is complaining about. – Maarten Aug 08 '13 at 08:36
  • Ok.As I said the form2 doesn't contain any control. – Mahesh KP Aug 08 '13 at 09:55
  • Can I try calling GC.Collect() at the dispose method of form2? Is it a goodway of coding? – Mahesh KP Aug 08 '13 at 10:50