Christophe Keller
Christophe Keller

Reputation: 834

C#: Form containing OpenFileDialog. Both call ShowDialog(). Memory Leak?

I have a form containing an OpenFileDialog added through the Visual Studio designer. I show the form using the following pattern:

using (var form = MyForm()) {
    form.ShowDialog();
}

Then in the form I call

myOpenFileDialog.ShowModal();

Because the OpenFileDialog was added through the designer I can't use the using pattern above. Furthermore, in the Designer.cs file it doesn't appear that

myOpenFileDialog.Dispose();

is called. Do I have a memory leak here?

Upvotes: 1

Views: 403

Answers (1)

fresky
fresky

Reputation: 540

There is no leak, but the OpenFileDialog will be disposed when the GC runs or the application went down. You can verify this by adding one function break point in visual studio for System.ComponentModel.Component.Dispose since the OpenFileDialog is derived from Component.

So I think it's better to dispose it by yourself when you no longer need it.

Upvotes: 1

Related Questions