.NETGURU
Do you need to dispose Brush objects?
Messages   Related Types
This message was discovered on ASPFriends.com 'ngfx-drawing' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Tim Anderson (itwriting.co.uk)
-- Copied from [winforms-cs] to [ngfx-drawing] by Click here to reveal e-mail address --Online help says:"It is important that you create a graphics resource only when you needit and release it soon as you are finished using it. If you create atype that implements the IDisposable interface, call its Dispose methodwhen you are finished with it in order to free resources." The help goeson to use Brush as an example.=20However, example code for owner draw list boxes, such as the followingfrom PDC, does not ever call the dispose method: =20private void colors_DrawItem(object sender,System.Windows.Forms.DrawItemEventArgs e) { if (e.Index =3D=3D -1) return; Rectangle bounds =3D e.Bounds; =20 e.DrawBackground(); using (Brush b =3D newSolidBrush((Color)colors.Items[e.Index])) { e.Graphics.FillRectangle(b, bounds); } if ((e.State & DrawItemState.Selected) !=3D 0) { using (Brush b =3D new HatchBrush(HatchStyle.Divot,SystemColors.Highlight, Color.Transparent)) { e.Graphics.FillRectangle(b, bounds); } } }---So my question is: should you always call Brush.Dispose, or is it notnecessary?Tim
Reply to this message...
 
    
Mitch Denny (VIP)
Tim,

Code samples are rarely if ever perfect, that goes
for code written by lads and lasses at Microsoft as well
as authors or anyone else.

If a class implements the IDisposable interface then
call Dispose. It doesn't do any harm.

----------------------------------------
- Mitch Denny
- http://www.warbyte.com
- Click here to reveal e-mail address
- +61 (414) 610-141
-

[Original message clipped]

Reply to this message...
 
    
Plash
Most sample code is written to demonstrate or high light a feature and some
times does not include ancillary code like erro handling, etc which is
necessary in production development but the amount of other code may mask
the point being made in the sample.

Plash
----------------------------------------------------------------------------
----
Sun em'bedded' java
dotNET put it to sleep

----- Original Message -----
From: "Mitch Denny" <Click here to reveal e-mail address>
To: "ngfx-drawing" <Click here to reveal e-mail address>
Sent: Wednesday, October 31, 2001 6:42 AM
Subject: [ngfx-drawing] RE: Do you need to dispose Brush objects?

[Original message clipped]

Reply to this message...
 
    
=?iso-8859-1?Q?Andreas_H=E4ber?=
Sorry if I'm wrong... but in the example you see the line
using (Brush b =3D new SolidBrush((Color)colors.Items[e.Index])) {
e.Graphics.FillRectangle(b, bounds);
}

if I'm not wrong then the using () clause automaticly calls dispose on the
object initialized in it (in this case b). This was one of the changes in C#
between beta 1 and beta 2.

hth,
Andreas Häber

----- Original Message -----
From: "Plash" <Click here to reveal e-mail address>
To: "ngfx-drawing" <Click here to reveal e-mail address>
Sent: Wednesday, October 31, 2001 3:41 PM
Subject: [ngfx-drawing] RE: Do you need to dispose Brush objects?

> Most sample code is written to demonstrate or high light a feature and
some
[Original message clipped]

Reply to this message...
 
 
System.Drawing.Brush
System.Drawing.Color
System.Drawing.Drawing2D.HatchBrush
System.Drawing.Drawing2D.HatchStyle
System.Drawing.SolidBrush
System.Drawing.SystemColors
System.IDisposable
System.Windows.Forms.DrawItemEventArgs
System.Windows.Forms.DrawItemState




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification