[AFTER DISCUSSION in comments to the answer]
If you did not pay attention on
System.IDisposal
, with System.Drawing you could possibly leak a
lot of memory!
This interface allows to call its method called
System.IDisposable.Dispose()
usually used to clean different things. Very typically it is used by
System.Drawing
to dispose unmanages resources.
Here is how you should do:
try {
Brush myBrush = new SolidBrush();
} finally {
myBrush.Dispose();
}
using (Brush myBrush = new SolidBrush() {
}
This is not just
Brush
.
Check up every single .NET library object you create if their types implement IDisposable
. When this is the case, you should
always use the codelet with "using" I've demonstrated above to avoid serious leak. I don't understand how you failed to come across this
important aspect before. (Maybe you're a beginner, so it's a very important thing to take into account.)
This construct and interface are one of the most important features if .NET. If you don't know it and trying to do GDI-based development, chances are, you lead a lot of unmanaged resources.
—SA