Question Is disposing a local brush variable necessary?


Sep 18, 2011
Programming Experience
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?
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.

Latest posts

Top Bottom