Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disposing Brushes

I am having a few memory problems with a long running application; I have been inspecting the paint methods to insure that brushes are properly disposed. In the case where the Brush is created in the argument to the function, will the brush be disposed after the call?

The case is outlined below:

     g.DrawString(valueText, Font, new SolidBrush(Color.Red),
like image 293
Brad Avatar asked Aug 30 '09 12:08

Brad


2 Answers

I am not entirely certain, but I don't believe it is. This would be safer:

using(var redBrush = new SolidBrush(Color.Red)
{
    g.DrawString(valueText, Font, redBrush);
}
like image 145
AJ. Avatar answered Sep 18 '22 00:09

AJ.


No, you should do it manually. Do however examine the classes Brushes and SystemBrushes, for ready-made brushes that you can use without creating new ones (and that you also don't need to / should not dispose).

like image 22
Fredrik Mörk Avatar answered Sep 19 '22 00:09

Fredrik Mörk