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

Do I need to dispose brush explicitly?

P: n/a
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
Share this Question
Share on Google+
2 Replies


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.