Brush Resource - Disposing

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When I instantiate a Brush resource such as a solidbrush, do I need to
dispose the resource when I'm finished or will it automatically be disposed
when the function/sub it's used in ends? Thank you for any replies.
 
Dennis said:
When I instantiate a Brush resource such as a solidbrush, do I need to
dispose the resource when I'm finished or will it automatically be
disposed
when the function/sub it's used in ends?

It won't be disposed automatically when the method ends. The GC will
dispose it when it does its cleanup. So, always call 'Dispose' when you
don't need a pen, brush, ... any more.
 
Dennis,
As Herfried stated: always call "Dispose" when you don't need a pen, brush,
.... any more

That you created!

If you got the brush from System.Drawing.Brushes or a pen from
System.Drawing.Pens you do not need to call Dispose on them, in fact you
will get an exception if you do, as System.Drawing.Brushes "owns" that brush
& its not nice to dispose of a resource you do not "own"... As that resource
may actually be shared within your app...

Hope this helps
Jay
 
Thanks Jay. In a sub, I have dimensioned a variable as "Brush" then use it
to define new brushes throughout the sub. Will the following properly
dispose of the brush or should I dispose of each new brush I create:

dim mybrush as Brush

mybrush = new solidbrush(Color.Red)
..................
mybrush = new solidbrush(Color.Blue)
.................
mybrush.dispose

or should I do the following:

dim mybrush as brush = new solidbrush(Color.Red)
.......
mybrush.dispose
dim mybrush as brush = new solidbrush(Color.Blue)
......
mybrush.dispose

Thanks.
 
Dennis,
For each brush you create (New SolidBrush) you need to call Dispose on it!

So Yes! you should do the following.
or should I do the following:

dim mybrush as brush = new solidbrush(Color.Red)
.......
mybrush.dispose
dim mybrush as brush = new solidbrush(Color.Blue)
......
mybrush.dispose

Otherwise the Red Brush will not be disposed of for a "long" time...


If there is the slightest chance that "......" will throw an exception you
should wrap the above in a Try/Finally...
dim mybrush as brush
Try
mybrush = new solidbrush(Color.Red)
....... Finally
mybrush.dispose
End Try

Try
mybrush = new solidbrush(Color.Blue)
...... Finally
mybrush.dispose
End Try

In VB.NET 2005 (aka Whidbey, due out later in 2005) there will be a new
Using statement that will simplify the Try/Finally Dispose pattern.

Using mybrush as brush = new solidbrush(Color.Red)
End Using

For info on VB.NET 2005 see http://lab.msdn.microsoft.com/vs2005/

Hope this helps
Jay
 
Thanks for your insight. That may be why my applications slows after a while
as I wasn't disposing of all the brushes and pens.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top