CA2000: object not disposed along all exception paths

3.8k Views Asked by At

Although topic has been discussed here before, but the proposed solutions don't seem to work..

I have a button-click-callback method in my form application, that shows a folder picker dialog:

private void ButtonSelectReporterFolderClick(object sender, EventArgs e)
{
    using (var dialog = new FolderBrowserDialog()) // causes warning
    {
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            this.boxReporterFolderPath.Text = dialog.SelectedPath;
        }
    }
}

This produces a warning:

CA2000: Microsoft.Reliability : In method 'MainWindow.ButtonSelectReporterFolderClick(object, EventArgs)', object '<>g__initLocal' is not disposed along all exception paths. Call System.IDisposable.Dispose on object '<>g__initLocal' before all references to it are out of scope.

I also tried using a try-finally block or even calling dialog.Dispose without any blocks, all to no avail - the warning remains, always at the line of initialization.

1

There are 1 best solutions below

3
On BEST ANSWER

The warning is not because FolderBrowserDialog is not disposed, it is because it has some public members that implements IDisposable interface and you're not disposing them separately. Of course, FolderBrowserDialog object knows how to dispose of it's dependencies but FxCop has no way of knowing that so it gives a warning. Just ignore the warning in your case.