views:

93

answers:

2

Hello:

I'm working with a .NET 2.0 WinForms application in C#.

I noticed something that I thought to be strange during the tear down of my application. In the designer generated dispose method:

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

I'm seeing a situation where it is passing in disposing = false parameter when components does indeed contain some items. This leads me to believe that these resources are not getting disposed correctly/released because components.Dispose(); is not getting called. Is this ever desired behavior?

Thanks.

+1  A: 

Maybe your class needs to dispose of unmanaged resources and therefor implements a finalizer as well?

Something like this:

   ~MyForm()
    {
       this.Dispose(false);
    }
Mez
+4  A: 

The disposing parameter will be passed as false if Dispose(bool) is being called from the finalizer. Typically this occurs when Dispose() hasn't been called.


The Dispose(bool) method isn't actually part of IDisposable; it's used by both IDisposable.Dispose() and by Object.Finalize(). The convention is that IDisposable.Dispose() will call Dispose(true) and Object.Finalize() will call Dipose(false).

STW
Is it ever implicitly called with true?
Ken
Awesome. You're right. Thanks for the explanation!
Ken