Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an object creating using an "inline" new statement automatically disposed?

Tags:

c#

The following snippet is taken from an example of ComboBox::DrawItem implementation on this MSDN page:

e.Graphics.FillRectangle(new SolidBrush(animalColor), rectangle);

I question this part: new SolidBrush(animalColor)

Since this is neither deliberately given a Dispose nor is it wrapped in a using, I assume this is also an example of poor form, since the SolidBrush object will be created and never disposed.

I have always labored under the assumption that I must use one of the aforementioned disposal mechanisms directly, or risk a memory leak.

Am I correct, or is there some deeper implicit disposal going on of which I am unaware? Perhaps because it was never assigned to a variable?

like image 843
DonBoitnott Avatar asked Jan 27 '15 19:01

DonBoitnott


People also ask

What do we call an object in C# code?

Objects are also called instances, and they can be stored in either a named variable or in an array or collection. Client code is the code that uses these variables to call the methods and access the public properties of the object.

Why should we dispose controller in flutter?

dispose method used to release the memory allocated to variables when state object is removed. For example, if you are using a stream in your application then you have to release memory allocated to the stream controller. Otherwise, your app may get a warning from the PlayStore and AppStore about memory leakage.

Is Dispose method called automatically flutter?

Flutter – dispose() Method with Example Dispose is a method triggered whenever the created object from the stateful widget is removed permanently from the widget tree. It is generally overridden and called only when the state object is destroyed.


2 Answers

It should be inside a using statement or it should explicitly call Dispose, but,

SolidBrush class inherits Brush class which has a destructor/finalize defined as:

Source from here:

   /**
     * Object cleanup
     */
    /// <include file='doc\Brush.uex' path='docs/doc[@for="Brush.Finalize"]/*' />
    /// <devdoc>
    ///    <para>
    ///       Releases memory allocated for this <see cref='System.Drawing.Brush'/>.
    ///    </para>
    /// </devdoc>
    ~Brush() 
    {
        Dispose(false);
    }

Once your object of class SolidBrush goes out of scope, Ultimately its destructor will be called. At that point Dispose will be called releasing any unmangaged resources.

See: Destructors (C# Programming Guide)

The destructor implicitly calls Finalize on the base class of the object.

The only issue with relying on destructor/finalizer is, that, you can't predict when the object will finally be disposed.

like image 130
Habib Avatar answered Sep 28 '22 22:09

Habib


I should say that's a poor example from msdn. You ought to Dispose it; no object will be disposed automatically.

It is not necessarily a leak, Brush implements a Finalizer which will Dispose it when garbage collected but you shouldn't rely on this.

like image 36
Sriram Sakthivel Avatar answered Sep 28 '22 23:09

Sriram Sakthivel