mustafabar
mustafabar

Reputation: 2359

do I have to call all the exposed dispose functions of the controls within a windows form in the overriden dispose function?

I have a windows form that contains many controls e.g timers, gridviews, and binding sources etc, and all of these expose a dispose function. Do I have to call their dispose function in this

        protected override void Dispose(bool disposing)
    {

        if (disposing && (components != null))
        {
            components.Dispose();
        }
        // do I have to write something here ???
        base.Dispose(disposing);
    }

also what does components.Dispose() exactly do?

thanks

Upvotes: 1

Views: 826

Answers (3)

Mike J
Mike J

Reputation: 3149

Generally when you add controls onto your forms via the toolbox in Visual Studio, the controls will automatically be Disposed for you (in the protected override void Dispose method).

The only time you do need to manually dispose objects is when you manually create other disposable objects that implement the IDisposable interface, things like file handles (Stream, StreamReader...), GDI objects (Bitmap, Brush) and unmanaged resources. Manually releasing objects of this type ensure you follow good coding practice by releasing the resources you create.

Upvotes: 2

Senthil Kumar
Senthil Kumar

Reputation: 479

The base.Dispose call invokes the System.Windows.Forms.Form.Dispose, which disposes menus and other form controls. The call ends up in System.Windows.Forms.Control.Dispose, which recursively disposes all child controls.

I guess you don't need to worry as long as your control is reachable from the form, either directly or transitively.

Upvotes: 0

Binary Worrier
Binary Worrier

Reputation: 51711

No, the form will call Dispose for you.

What is Dispose for? This is taken from here IDispose

Use the Dispose method of this interface to explicitly release unmanaged resources in conjunction with the garbage collector. The consumer of an object can call this method when the object is no longer needed

Upvotes: 0

Related Questions