Avoiding disposing system-defined Pen and Brush instances?

There is no requirement to call Dispose The purpose of garbage collection is to eliminate these kinds of requirements One of the main purposes of IDisposable is to allow a class to clean up unmanaged resources in resource-limited environments. If you do not call the dispose method, the unmanaged resouces of the class will be cleaned up once the object is finialized and disposed during garbage collection If you "must" call dispose and you do not know if the brush instance is a "system-" or a "normal-" brush then you will have to use a try...catch block It is not needed to call dispose on SystemBrushes and SystemPens because the GDI+ Library will take care of these resources Okay to dispose of SystemFonts and SystemIcons The Remarks section of the class will make note of if there is a requirement to call the Dispose method If the Remarks section recommends to call the Dispose method, then I will do it In general, I do not call dispose on pens and brushes. If I have a graphic-intensive application or class then I will cache instances of the pens and brushes I need.

I use them throughout the life of the application or class. If I didn't do this then graphics painting performance will suffer trying to create and dispose all those ojects so many times and so frequently. (Hm...now that I think about it, performance is probably why we cannot dispose of SystemBrushes and SystemPens, yet can dispose of SystemFonts and SystemIcons.

Even the framework caches SystemBrushes and SystemPens. ).

There is no requirement to call Dispose. The purpose of garbage collection is to eliminate these kinds of requirements. One of the main purposes of IDisposable is to allow a class to clean up unmanaged resources in resource-limited environments.

If you do not call the dispose method, the unmanaged resouces of the class will be cleaned up once the object is finialized and disposed during garbage collection. If you "must" call dispose and you do not know if the brush instance is a "system-" or a "normal-" brush then you will have to use a try...catch block. It is not needed to call dispose on SystemBrushes and SystemPens because the GDI+ Library will take care of these resources.

Okay to dispose of SystemFonts and SystemIcons. The Remarks section of the class will make note of if there is a requirement to call the Dispose method. If the Remarks section recommends to call the Dispose method, then I will do it.In general, I do not call dispose on pens and brushes.

If I have a graphic-intensive application or class then I will cache instances of the pens and brushes I need. I use them throughout the life of the application or class. If I didn't do this then graphics painting performance will suffer trying to create and dispose all those ojects so many times and so frequently.

(Hm...now that I think about it, performance is probably why we cannot dispose of SystemBrushes and SystemPens, yet can dispose of SystemFonts and SystemIcons. Even the framework caches SystemBrushes and SystemPens.).

Thanks for your comments, however I think it needs clarifying that the GC will only clean up unmanaged resources if you have a finalizer declared - it doesn't automatically call your Dispose() method. Applications can pre-empt the GC by calling Dispose() which should then allow it to be GC'd sooner. Otherwise, I suspect you are right and try/catch is the only option if there is no other way of knowing what the brush/pen was set to.

– David Gardiner Jul 12 '10 at 8:09 Note that nearly all GDI+ based classes implement the IDisposable interface because of their use of unmanaged resources. Therefore, in regards to this question and answer, Dispose(disposing=False) will always be called through the Finalize method. Regardless, there is still no requirement to call Dispose.

– AMissico Jul 12 '10 at 10:33 The framework automatically calls the Dispose methods of any created SystemBrushes and SystemPens objects when the GDI+ library is shut-down. – AMissico Jul 12 '10 at 10:35 I did not use the word "automatically". I left out the details because this is not part of the question/answer.

– AMissico Jul 12 '10 at 10:36 Unfortunately, the remarks in this case are incomplete. Look at the sample code in the documentation -- it does explicitly call Dispose, as it should. Actually, that first statement works as a general rule.

If remarks were that complete, wouldn't we have less need for sites like this one? :) – Ari Roth Jul 12 '107 at 5:43.

First of all, you always should Dispose of brushes when you can and not leave it up to the garbage collector. While GDI will eventually get around to taking care of that stuff (assuming the library gets shut down properly), there's no telling when that may be. In fact, my understanding is that brush handles stick around for the long-term.

Over the course of a long-running application, you're looking at a de facto memory leak. Sure, in a small application that won't run for long, or in one that only rarely creates brushes, you can let the system handle it, but that strikes me as sloppy. As a general rule, whenever I create a brush I do so in a using statement.

That automatically calls dispose on the brush without having to worry about it. As an added bonus, since you create the brush inside the statement you know that it's not a predefined brush. Any time you create and use a non-predefined brush, wrap the block in a using and you don't have to worry about it at all.In fact, you don't even need to explicitly call Dispose, since the block will do so even in the case of an exception.

I could be wrong but I think you can assume that the lifetime (and the disposal) of the predefined brushes and pens is not your app's responsibility and will be handled by the system. In short: don't call Dispose on the predefined stuff. :).

Yes, that's what I want to avoid - my question is how to know if calling Dispose() is ok or not. I'm interested if there's anything better than calling Dispose() inside a try/catch, because I'd prefer to avoid the 'cost' of having an exception thrown. – David Gardiner Jul 8 '10 at 5:15 @Whoever: Why the downvote?

Lzcd is correct. You can verify through Microsoft's source code or . NET Reflector.

– AMissico Jul 12 '10 at 10:36.

Only thing come in mind is to have a practice do not use System pens/brushes as parameters to methods.

Just for completeness, using Reflector I see that the System.Drawing. Pen and System.Drawing. SolidBrush classes have a private field named 'immutable'.

This field appears to be set to true when the object refers to a system-defined resource, so you could use reflection to carefully check the value of this field to decide whether to call Dispose() on it.

Short answer is,.. if you create it, either delegate responsibility to clean up or clean the objects up yourselves. You can create GDI resource "leaks" by letting things hang in the garbage collector. They may eventually be cleaned up, but they are not doing any good hanging there.I.e.

If you don't call "close" or Dispose on opened files, the file's remain locked until the GC "gets around to it.

If you "must" call dispose and you do not know if the brush instance is a "system-" or a "normal-" brush then you will have to use a try...catch block. It is not needed to call dispose on SystemBrushes and SystemPens because the GDI+ Library will take care of these resources. Okay to dispose of SystemFonts and SystemIcons.

The Remarks section of the class will make note of if there is a requirement to call the Dispose method. If the Remarks section recommends to call the Dispose method, then I will do it. In general, I do not call dispose on pens and brushes.

If I have a graphic-intensive application or class then I will cache instances of the pens and brushes I need. I use them throughout the life of the application or class. If I didn't do this then graphics painting performance will suffer trying to create and dispose all those ojects so many times and so frequently.

(Hm...now that I think about it, performance is probably why we cannot dispose of SystemBrushes and SystemPens, yet can dispose of SystemFonts and SystemIcons.

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