Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A better Dispose method ( through ComponentModel.IContainer ?? )

Tags:

c#

.net

memory

I have this Dispose method I want to change. (Yeah I should check every object for null, I know)

    protected override void Dispose(bool disposing)
    {
        if( disposing )
        {
            if( monthLineBrush != null)
                monthLineBrush.Dispose();
            monthHeaderLineBrush.Dispose();
            shadowBrush.Dispose();
            monthHeaderLineBrushDark.Dispose();
            monthFontBrush.Dispose();
            weekendBgBrush.Dispose();
            whiteBrush.Dispose();
            dayFontBrush.Dispose();
            chartBrush.Dispose();
            chartWarningBrush.Dispose();
            barBrush.Dispose();
            monthLinePen.Dispose();
            monthHeaderLinePen.Dispose();
            monthHeaderLinePenDark.Dispose();
            warningLinePen.Dispose();
            monthFont.Dispose();
            yearFont.Dispose();
            weekLinePen.Dispose();
            dayLinePen.Dispose();
            tooltip.Dispose();
            toolTipLabel.Dispose();
        }

        base.Dispose(disposing);
    }

VS uses a System.ComponentModel.IContainer object named components , and only disposes this components object. Yet I can't find any code where the different objects are added to the components objedct ??

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

How does this work ?

like image 968
Run CMD Avatar asked Jul 12 '26 07:07

Run CMD


1 Answers

The code to add each object that is created to the components container is automatically generated and added to the *.Designer.cs file associated with each form by the designer.

For example, the Form1.Designer.cs file might look something like this in a brand new, empty project once you've added a ToolTip control to the form:

private void InitializeComponent()
{
    this.components = new System.ComponentModel.Container();
    this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
    this.SuspendLayout();
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(284, 264);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);
}

There are two relevant lines of code in that whole mess, the two that appear at the very top:

this.components = new System.ComponentModel.Container();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);

The first line creates a System.ComponentModel.Container object called components, to which all of the components that implement IDisposable will be added when they are created.

The second line creates a ToolTip object (in response to one being drug to the form's surface during design time), and uses a constructor overload that accepts a parameter of type IContainer.

Extending this same song and dance to the other components that are added to your form allows them all to be disposed at one go with the Dispose method provided by the components container.

like image 144
Cody Gray Avatar answered Jul 13 '26 20:07

Cody Gray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!