views:

64

answers:

2

I am having a few memory problems with my long running application and I have been inspecting the paint methods to insure that brushes were properly disposed. In the case where the Brush is created in the argument to the functio will the brush be disposed of?

such as the case below

     g.DrawString(valueText, Font, new SolidBrush(Color.Red),
+4  A: 

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);
}
AJ
You can be certain.
Henk Holterman
+2  A: 

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).

Fredrik Mörk
You cannot Dispose the stock brushes. If you try it will throw.
Henk Holterman