Question Is disposing a local brush variable necessary?

priyamtheone

New member
Joined
Sep 18, 2011
Messages
3
Programming Experience
Beginner
MSDN recommends disposing any variable of type System.Drawing.Brush before its last reference is released. Otherwise, the resources it is using will not be freed until the garbage collector calls the Brush object's Finalize method.

As we know, local variables are destroyed automatically when the control flow goes out of the scope of the method it belongs. So, is it necessary to dispose a brush object every time if it is local?
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
815
Location
Norway
Programming Experience
10+
Yes, it is always necessary to dispose objects before you 'release' them. That the variable goes out of scope just means that the memory allocated to that variable is freed, the object it referred to (and its memory) lives on, and it can take a long time before GC is finalizing objects.
Also read the remarks for Brushes class.
 
Top Bottom