473,385 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Do I need to dispose brush explicitly?

Hi all

In following code, is it OK not to dispose local brush explicitly before
exiting the procedure?

private void MyDrawItem ( Graphics g, Color BackColor, Rectangle r )
{
g.FillRectangle(new SolidBrush(backColor), r);
}

Thanks
Alex
Sep 21 '06 #1
2 3548
Alex,

Yes, you do. You should be doing this:

private void MyDrawItem ( Graphics g, Color BackColor, Rectangle r )
{
using (SolidBrush solidBrush = new SolidBrush(backColor))
{
g.FillRectangle(solidBrush, r);
}
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alex K." <Al***@discussions.microsoft.comwrote in message
news:55**********************************@microsof t.com...
Hi all

In following code, is it OK not to dispose local brush explicitly before
exiting the procedure?

private void MyDrawItem ( Graphics g, Color BackColor, Rectangle r )
{
g.FillRectangle(new SolidBrush(backColor), r);
}

Thanks
Alex

Sep 21 '06 #2
Certainly. See the GDI+ FAQ for more info on graphical object lifetimes.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Alex K." <Al***@discussions.microsoft.comwrote in message
news:55**********************************@microsof t.com...
Hi all

In following code, is it OK not to dispose local brush explicitly before
exiting the procedure?

private void MyDrawItem ( Graphics g, Color BackColor, Rectangle r )
{
g.FillRectangle(new SolidBrush(backColor), r);
}

Thanks
Alex

Sep 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: qrli | last post by:
I used to think that all objects that implement IDisposable should be disposed. But I found 80% of the classes implement IDisposable. But when I looked into the samples, most objects are not...
5
by: MFC | last post by:
Ok, after three C# books, (C# How to Program, Programming in the Key of C#, and C# Weekend Crash Course) and three weeks, I believe I have tried everything to make a certain form function...
15
by: Sam Sungshik Kong | last post by:
Hello! A disposable object's Dispose() method can be called either explicitly by the programmer or implicitly during finalization. If you call Dispose, the unmanaged resources are released...
6
by: Fernando Cacciola | last post by:
Help me out here please: While watching Brad Abraham's MSDN TV talk about the Dispose pattern, refering to: public virtual void Dispose ( bool disposing ) { if ( disposing ) { <-- WHAT...
8
by: Pierson C | last post by:
I am developing on a website that is utilizing SQL Server 2000. Shortly after deploying the site, we began having timeout issues due to the max connections. 1st instinct was to diligently tidy...
14
by: Jonas | last post by:
Hi! I'm developing the middletiers of an ASP.NET application in VB.NET. I've got a business logic layer in which I would like to perform auditing to a database. Instead of making an auditing...
6
by: Water Cooler v2 | last post by:
I heard from someone that we must not implement IDisposable for all classes. Can someone please tell me: 1. the reason why we must not implement IDisposable for all the classes we write. 2....
25
by: Thore Berntsen | last post by:
I'm trying to understand the IDisposable pattern. In the sample on MSDN they have added a destructor/finalizer like this: ~MyResource() { Dispose(false); } Calling Dispose(false) will only...
11
by: Rainer Queck | last post by:
Hi NG, just a short question: If a class implements the IDisposable interface, does the garbage collector always call the Dispose() method of the class, before it is cleaned up? Regards...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.