When you have code like:
Bitmap bmp = new Bitmap ( 100, 100 );
Graphics g = Graphics.FromImage ( bmp );
Pen p = new Pen ( Color.FromArgb ( 128, Color.Blue ), 1 );
Brush b = new SolidBrush ( Color.FromArgb ( 128, Color.Blue ) );
g.FillEllipse ( b, 0, 0, 99, 99 );
g.FillRegion ( b, pictureBox1.Region );
pictureBox1.BackColor = Color.Transparent;
pictureBox1.Image = bmp;
Do you have to dispose the pen and brush? What about bmp and the g?
My main question is, if these were to be disposed manually, why don't they get disposed as soon as they get out of the scope? Is that what would happen, if you didn't dispose them manually? Is it the delay that makes people do this manually?
C# doesn't "destruct" or dispose of things as soon they go out of scope.
Those classes will most likely automatically free the unmanaged resources that they hold on to in their special
Finalizer
method, which will be called when they are garbage collected at an indeterminate time after going out of scope.But to rely on that is to rely on something that is out of your control, and might not happen for a while.
If the class implements IDisposable, best practice is for you to manually call
Dispose()
somewhere, or preferably wrap it in ausing
block. That way you can be sure that:Dispose is used to dispose of unmanaged resources.
So, as a rule of thumb, I wrap any instantiations of IDisposable objects in a using statement, so I don't have to worry about what unmanaged resource a Pen has.
If the disposable pattern is used correctly,
Dispose
is not strictly necessary -- it will be called when the object is finalized, so you won't leak resources or anything.However, it's good manners to call
Dispose
as soon as you're done using the object, as disposables often directly control native resources which are usually limited. Objects don't normally get finalized/collected right away, so those resources are just hanging around, wasted, once you're not using the object anymore. Disposing frees those resources immediately, so they can be used by other parts of the program (or in some cases, by other programs).Note, a
using
block automatically disposes the object when you're done with it, which is why you rarely if ever seeDispose
within ausing
block.Short version: If the object implements
IDisposable
, and your code created it (ie: if it's not a system object likePens.Blue
, or theGraphics
you get passed inOnPaint
etc), it should be disposed when you're totally done with it -- whether by callingDispose
, or by calling some other method that's specified to callDispose
(Close
is a common one), or by using ausing
block. You don't have to dispose it, but you almost always should.Yes, bmp, g, b and p are all IDisposable, you should Dispose() all of them. Preferably by using
using() {}
blocks.There are exceptions, when you use
Pen p2 = Pens.Blue;
yous should not dispose p2. It's called a stock item. The same for Brushes.Black etc.As for the why, it's the same for all disposable classes. .Net does not use reference counting so there is no (cannot be) an immediate action when a reference goes out of scope.
And leaving it to the Garbage collector will eventually free them but it is (very) inefficient. I know of an ASP.NET (!) application that failed on a shortage of Graphic handles because of not promptly disposing them. It was generating images.
I know other people have put code examples here, but I started so I'll finish:
I always use
using
in my code since it callsDispose()
on your object automatically, even if there is an exception raised in theusing
block. I use it a lot for SharePoint projects (but that's another story...).Yes, you have to dispose them - not just pen and brush, but also
Bitmap
andGraphics
.They don't get disposed when they're out of scope because the variables themselves are references, not objects, and C# compiler doesn't know whether the ownership still belongs to those references or not (e.g.
FillEllipse
could, in theory, remember the reference it's given, and try to use it at some later moment - remember that language compiler doesn't have any special knowledge of library semantics!).If you want to indicate that ownership is restricted to that scope, you use the
using
statement:This will make the compiler insert calls to
Dispose
automatically as needed, ensuring that all objects are disposed once the correspondingusing
scope is left (whether normally, by control transfer such asreturn
orbreak
, or an exception).If you come from a C++ background,
using
in C# is directly analogous to aconst std::auto_ptr
, except that it's a language construct here, and can only be used for local variables (i.e. not for class fields).