By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,337 Members | 2,234 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,337 IT Pros & Developers. It's quick & easy.

Brush Resource - Disposing

P: n/a
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 in Houston
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Dennis" <De****@discussions.microsoft.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #2

P: n/a
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
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:73**********************************@microsof t.com...
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 in Houston

Nov 21 '05 #3

P: n/a
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.

"Jay B. Harlow [MVP - Outlook]" wrote:
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
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:73**********************************@microsof t.com...
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 in Houston


Nov 21 '05 #4

P: n/a
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

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:3E**********************************@microsof t.com... 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.

"Jay B. Harlow [MVP - Outlook]" wrote:
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
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:73**********************************@microsof t.com...
> 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 in Houston


Nov 21 '05 #5

P: n/a
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.

"Jay B. Harlow [MVP - Outlook]" wrote:
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

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:3E**********************************@microsof t.com...
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.

"Jay B. Harlow [MVP - Outlook]" wrote:
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
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:73**********************************@microsof t.com...
> 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 in Houston


Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.