Disposing a static brush?

It looks like you're trying to dispose of a static, which causes some problems next time it's used: Brush brush = Brushes. Black; g. FillEllipse(brush, 3, 3, 2, 2); //Here the exception is thrown on the second call to the function brush.Dispose(); //If I comment this out, it works ok When you set brush = Brushes.

Black, you're actually setting brush as a reference (or pointer) to the static Brushes.Black. By disposing it, you're effectively writing: Brushes.Black.dispose() When you come back around to use the black brush again, the runtime says you can't because it's already been disposed of, and isn't a valid argument to g.FillEllipse() A better way to write this might be just simply: g. FillEllipse(Brushes.

Black, 3, 3, 2, 2) Or, if you want to be really complex about it: Brush brush = Brushes.Black.Clone(); g. FillEllipse( brush, 3, 3, 2, 2 ); brush.Dispose() Or if you don't care about things looking wrong, just comment out the brush.Dispose(); line in your original code.

It looks like you're trying to dispose of a static, which causes some problems next time it's used: Brush brush = Brushes. Black; g. FillEllipse(brush, 3, 3, 2, 2); //Here the exception is thrown on the second call to the function brush.Dispose(); //If I comment this out, it works ok.

When you set brush = Brushes. Black, you're actually setting brush as a reference (or pointer) to the static Brushes.Black. By disposing it, you're effectively writing: Brushes.Black.dispose(); When you come back around to use the black brush again, the runtime says you can't because it's already been disposed of, and isn't a valid argument to g.FillEllipse() A better way to write this might be just simply: g.

FillEllipse(Brushes. Black, 3, 3, 2, 2); Or, if you want to be really complex about it: Brush brush = Brushes.Black.Clone(); g. FillEllipse( brush, 3, 3, 2, 2 ); brush.Dispose(); Or if you don't care about things looking wrong, just comment out the brush.Dispose(); line in your original code.

Thanks all, I get it now :) I wish they would make this a compile-time error... – Spikolynn Jan 23 '09 at 3:24.

Bruhes. Black is a system resource, and is not intended for you to dispose. The runtime manages the brushes in the Brushes class, the Pens, and other such objects.It creates and disposes those objects as required, keeping frequently-used items alive so that it doesn't have to continually create and destroy them.

The documentation for the Brushes class says: The Brushes class contains static read-only properties that return a Brush object of the color indicated by the property name. You typically do not have to explicitly dispose of the brush returned by a property in this class, unless it is used to construct a new brush.In short, do not call Dispose on the system-supplied objects.

More accurately, do not call Dispose on Static objects. – Adam N Feb 6 '09 at 1:00.

I don't think you need to call . Dispose on static brushes, only if you create new ones. Although, personally, I would use the using syntax.. ie: using (Brush brush = new SolidBrush(...)) { g.

FillEllipse(brush, 3, 3, 2, 2); } And you should probably do the same thing with the graphics object you create.

Yes, do not attempt to dispose of static brushes. I had a hell of a time tracking this one down once. – Ed S.

Jan 23 '09 at 2:26 Wouldn't your code just set brush as a reference to the static brush, and then dispose it after the using block? – Jon B Jan 23 '09 at 2:35 No, he is creating a new brush here. – Ed S.

Jan 23 '09 at 17:58 I updated the code after the post, so he wasn't wrong. – FryGuy Jan 23 '09 at 2:31.

Brush brush = Brushes. Brush.Dispose(); //If I comment this out, it works ok. When you set brush = Brushes.

Black, you're actually setting brush as a reference (or pointer) to the static Brushes.Black. Or if you don't care about things looking wrong, just comment out the brush.Dispose(); line in your original code. Black is a system resource, and is not intended for you to dispose.

The runtime manages the brushes in the Brushes class, the Pens, and other such objects. It creates and disposes those objects as required, keeping frequently-used items alive so that it doesn't have to continually create and destroy them. The property name.

A new brush. In short, do not call Dispose on the system-supplied objects. I don't think you need to call .

Dispose on static brushes, only if you create new ones. And you should probably do the same thing with the graphics object you create. Terms of service.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions