Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CA2000: object not disposed along all exception paths

Tags:

c#

fxcop

ca2000

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.

like image 686
Efrain Avatar asked Jul 22 '11 11:07

Efrain


1 Answers

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.

like image 131
Teoman Soygul Avatar answered Oct 15 '22 16:10

Teoman Soygul