473,782 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Brush Resource - Disposing

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
5 2285
"Dennis" <De****@discuss ions.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
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****@discuss ions.microsoft. com> wrote in message
news:73******** *************** ***********@mic rosoft.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
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(Colo r.Red)
............... ...
mybrush = new solidbrush(Colo r.Blue)
............... ..
mybrush.dispose

or should I do the following:

dim mybrush as brush = new solidbrush(Colo r.Red)
.......
mybrush.dispose
dim mybrush as brush = new solidbrush(Colo r.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****@discuss ions.microsoft. com> wrote in message
news:73******** *************** ***********@mic rosoft.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
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(Colo r.Red)
.......
mybrush.dispose
dim mybrush as brush = new solidbrush(Colo r.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(Colo r.Red) ....... Finally mybrush.dispose End Try

Try
mybrush = new solidbrush(Colo r.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(Colo r.Red) ...... End Using

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

Hope this helps
Jay

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:3E******** *************** ***********@mic rosoft.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(Colo r.Red)
............... ...
mybrush = new solidbrush(Colo r.Blue)
............... ..
mybrush.dispose

or should I do the following:

dim mybrush as brush = new solidbrush(Colo r.Red)
.......
mybrush.dispose
dim mybrush as brush = new solidbrush(Colo r.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****@discuss ions.microsoft. com> wrote in message
news:73******** *************** ***********@mic rosoft.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
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(Colo r.Red)
.......
mybrush.dispose
dim mybrush as brush = new solidbrush(Colo r.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(Colo r.Red)
.......

Finally
mybrush.dispose

End Try

Try
mybrush = new solidbrush(Colo r.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(Colo r.Red)
......

End Using

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

Hope this helps
Jay

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:3E******** *************** ***********@mic rosoft.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(Colo r.Red)
............... ...
mybrush = new solidbrush(Colo r.Blue)
............... ..
mybrush.dispose

or should I do the following:

dim mybrush as brush = new solidbrush(Colo r.Red)
.......
mybrush.dispose
dim mybrush as brush = new solidbrush(Colo r.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****@discuss ions.microsoft. com> wrote in message
news:73******** *************** ***********@mic rosoft.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4864
by: Rene | last post by:
I would like to get a reference to a Brush using the color name of the brush. For example, right now if I want a brush with a certain color, I would hard code something like Brushes.PeachPuff, this will get me a PeachPuff brush, but what if my brush depended on a string passed by the user? If all I had was a string with the name "PeachPuff" how can I create Brushes.PeachPuff using the string value? Thank you.
2
43462
by: boxim | last post by:
having a moment, cant you just Brush br = new Brush(Color.Black);??? saying you cant cos it's an abstract class need to get to a brush from a color tia sam martin
5
2108
by: Barry Anderberg | last post by:
I'm using a tool by Sci-Tech called the .NET Memory Profiler. We have a massive .NET/C# application here and it has been exhibiting memory leak behavior for some time. Attempting to remedy the problems I am employing the aforementioned software in trying to track down the problems. After an hour or so of program execution, a snapshot of the managed heap shows 27,025 BinaryReader objects as having been garbage collected but never...
3
1735
by: Saradhi | last post by:
How to get the names of all the Resources embedded in a Resource File? -SARADHI
3
1999
by: Scott Meddows | last post by:
I've added a BMP file to my project file in Visual Studio and told the compiler to embed the file into the binary. How do I now read this file back into a filestream? Thanks
3
2278
by: jcrouse | last post by:
I have the following string: e.Graphics.DrawString(Label5.Text, lblP1B1.Font, Brushes.White, -y, 0) I want to use the property label.forecolor for my text color. How do I get whats in label.forecolor into the Brushes.Color parameter? Thank you, John
2
1539
by: Johnny J. | last post by:
I've got an inherited control where I want the user to be able to specify a color property. Using that color, I'm drawing a line private m_UserDefinedColor as Color ..... Dim g as Graphics=this.CreateGraphics Dim myPen as Pen = new Pen(m_UserDefinedColor) g.DrawLine(myPen, X1, Y, X2 - 1, Y)
0
1282
by: steve | last post by:
When drawing with a hatch brush with a statement such as myGraphics.FillPolygon(hatchBrush, polygon.vertices); the hatch brush is always oriented in the same direction. Is there a way of rotating the brush so that I can give it a different orientation for each polygon? I.e. if it was a triangle and it was drawn upright then the brush would appear as normal but if the triangle was drawn upside down the hatch brush pattern would...
31
3417
by: Tom P. | last post by:
I am doing quite a bit of custom painting and it means I have to create a lot of brushes (think one for every file system object in a directory) per paint. How expensive is this? Should I find a way to create the brushes once, store them in an member variable, and use them when I need them? Or is creating brushes a throw-away process that doesn't take a lot of work? Thanks for the info. Tom P.
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.