Results 1 to 9 of 9

Thread: [RESOLVED] Is disposing a local brush variable necessary?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

    Resolved [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?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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

  3. #3
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    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

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Is disposing a local brush variable necessary?

    Quote Originally Posted by boops boops View Post
    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.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

    Re: Is disposing a local brush variable necessary?

    Thanks!

  8. #8
    Lively Member
    Join Date
    Sep 2013
    Posts
    117

    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.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    16

    Resolved Re: Is disposing a local brush variable necessary?

    Quote Originally Posted by AdamPanic2013 View Post
    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
  •  



Click Here to Expand Forum to Full Width