When is Dispose necessary?

2019-01-14 16:58发布

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?

6条回答
相关推荐>>
2楼-- · 2019-01-14 17:24

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 a using block. That way you can be sure that:

A. The unmanaged resources are definitely being freed.

B. The unmanaged resources are freed as soon as possible.

查看更多
爷的心禁止访问
3楼-- · 2019-01-14 17:24

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.

查看更多
霸刀☆藐视天下
4楼-- · 2019-01-14 17:28

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 see Dispose within a using block.

Short version: If the object implements IDisposable, and your code created it (ie: if it's not a system object like Pens.Blue, or the Graphics you get passed in OnPaint etc), it should be disposed when you're totally done with it -- whether by calling Dispose, or by calling some other method that's specified to call Dispose (Close is a common one), or by using a using block. You don't have to dispose it, but you almost always should.

查看更多
地球回转人心会变
5楼-- · 2019-01-14 17:36

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.

查看更多
The star\"
6楼-- · 2019-01-14 17:42

I know other people have put code examples here, but I started so I'll finish:

using (Bitmap bmp = new Bitmap(100, 100))
{
  using (Graphics g = Graphics.FromImage(bmp))
  {
    using (Pen p = new Pen(Color.FromArgb(128, Color.Blue), 1))
    {
      using (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;
      }
    }
  }
}

I always use using in my code since it calls Dispose() on your object automatically, even if there is an exception raised in the using block. I use it a lot for SharePoint projects (but that's another story...).

查看更多
爷、活的狠高调
7楼-- · 2019-01-14 17:50

Yes, you have to dispose them - not just pen and brush, but also Bitmap and Graphics.

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:

using (Bitmap bmp = new Bitmap ( 100, 100 ))
using (Graphics g = Graphics.FromImage ( bmp ))
using (Pen p = new Pen ( Color.FromArgb ( 128, Color.Blue ), 1 ))
using (Brush b = new SolidBrush ( Color.FromArgb ( 128, Color.Blue ) ))
{
    g.FillEllipse ( b, 0, 0, 99, 99 );    
    g.FillRegion ( b, pictureBox1.Region );
}

This will make the compiler insert calls to Dispose automatically as needed, ensuring that all objects are disposed once the corresponding using scope is left (whether normally, by control transfer such as return or break, or an exception).

If you come from a C++ background, using in C# is directly analogous to a const 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).

查看更多
登录 后发表回答