-
Nov 9th, 2013, 10:04 AM
#1
Thread Starter
Junior Member
[RESOLVED] Is disposing a local brush variable necessary?
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?
-
Nov 9th, 2013, 11:53 AM
#2
Re: Is disposing a local brush variable necessary?
If I remember right, brushes are actually operating system resources, and therefore are in somewhat more short supply than some other types of resources, so you certainly want to clean them up properly.
The local variable is certainly destroyed when the method exits. The local variable holds the address of the brush, since a brush is a reference type (at least I would assume that it is), so you are destoying the address of the object. Are you destoying the actual object? Probably not.
My usual boring signature: Nothing
-
Nov 9th, 2013, 12:20 PM
#3
Re: Is disposing a local brush variable necessary?
I've never heard of "operating system resources" but the System.Drawing.Brush class seems to me an unlikely candidate for that description. Perhaps you are thinking of members of the Brushes class (Brushes.Red etc.) which inherit from System.Object instead of System.Drawing.Brush like all other GDI+ brushes; you can in fact dispose of them but I don't see how you could gain anything by doiong so.
You can make SolidBrush members with any combination of ARGB as well as LinearGradientBrushes and PathGradientBrushes with arrays of colours.
And then there is the TextureBrush, which has an Image argument. I suspect that the last is the only brush type which needs some care, since the existence of a TextureBrush could prevent the associated image from being disposed, which could be expensive. Could that lockup continue past the scope of the block where the TextureBrush is declared? It seems unlikely to me.
Members of the Pen class can be made from Brushes, including TextureBrushes. So if caution is needed, I suppose it applies as much (if at all) to a Pen as a TextureBrush.
Personally, I always use a Using-End Using block for Brushes, Pens and any other kind of object that is no longer needed and is disposable. But that's just because I think it adds clarity.
BB
-
Nov 9th, 2013, 05:48 PM
#4
Re: Is disposing a local brush variable necessary?
It's a 20 year old memory from Petzold's Programming Windows. It's reasonably likely that I have it wrong by now. There certainly isn't any harm in disposing of variables that have a .Dispose method.
My usual boring signature: Nothing
-
Nov 9th, 2013, 09:36 PM
#5
Re: Is disposing a local brush variable necessary?
Brushes, Pens and Bitmaps all hold on to unmanaged resources. The GC cannot account for these resources which is why Dispose was provided for these objects. If these objects use too much memory, the GC won't know. It can only know when managed objects are using too much memory. This makes it possible to run out of memory if the GC cleans up less than its supposed to because of not accounting for unmanaged resources being referenced by managed objects.
So while you can get away with not disposing of Brushes or Pens and such, you should practice disposing of them. Especially in cases where you may be allocating a large number of them of short periods of time like inside a loop.
-
Nov 9th, 2013, 10:21 PM
#6
Re: Is disposing a local brush variable necessary?
Originally Posted by boops boops
I've never heard of "operating system resources" but the System.Drawing.Brush class seems to me an unlikely candidate for that description. Perhaps you are thinking of members of the Brushes class (Brushes.Red etc.) which inherit from System.Object instead of System.Drawing.Brush like all other GDI+ brushes; you can in fact dispose of them but I don't see how you could gain anything by doiong so.
The Brushes class is just a convenient way to access "standard" Brush objects. The Brushes.Red property returns a System.Drawing.Brush object, specifically a SolidBrush with a Color of Red.
-
Nov 23rd, 2013, 11:00 AM
#7
Thread Starter
Junior Member
Re: Is disposing a local brush variable necessary?
-
Nov 23rd, 2013, 01:54 PM
#8
Lively Member
Re: Is disposing a local brush variable necessary?
Generally anything that implements IDisposable holds a references to a limited OS resource and usually this contains unmanaged code). Sometimes they're are also pinned, cause they are typically calling a C / C++ interface that doesn't expected the addressed passed to it to change (cos it's unmanaged). Since it's pinned that object can be moved in memory, and thus garbage collector can move it to compact the heap.
So always dispose of the disposable objects.
Code:
Using ido As ' some IDisposable
'' blah, blah
End Using ' Automatically handles the disposing for you.
-
Dec 8th, 2013, 10:20 AM
#9
Thread Starter
Junior Member
Re: Is disposing a local brush variable necessary?
Originally Posted by AdamPanic2013
Generally anything that implements IDisposable holds a references to a limited OS resource and usually this contains unmanaged code). Sometimes they're are also pinned, cause they are typically calling a C / C++ interface that doesn't expected the addressed passed to it to change (cos it's unmanaged). Since it's pinned that object can be moved in memory, and thus garbage collector can move it to compact the heap.
So always dispose of the disposable objects.
Code:
Using ido As ' some IDisposable
'' blah, blah
End Using ' Automatically handles the disposing for you.
Thanks for the explanation!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|