Dispose vs Dispose(bool)

I am confused about dispose. I am trying to get my code disposing resources correctly. So I have been setting up my classes as IDisposable (with a Dispose method) them making sure that the Dispose method gets called.

But now FXCop is telling me lots of stuff about Disposing = false and calling Dispose(false).

I don't see a Dispose method that takes a bool. Do I need to make one? If so, why? Why not just have a method that gets called when it is disposing?

I saw some code here: http://msdn.microsoft.com/en-us/library/ms244737.aspx that shows how to make a Disposing method that takes a bool. It says it is for native vs managed resourses. But I thought the whole point of dispose was for unmanaged resourses only.

Also, the line that FXCop is complaining about is this:

    ~OwnerDrawnPanel()
    {
        _font.Dispose();
    }

It says:

CA1063 : Microsoft.Design : Modify 'OwnerDrawnPanel.~OwnerDrawnPanel()' so that it calls Dispose(false) and then returns.

But Font does not have a Dispose(bool) on it (that I can find).

To sum it up:

Why do I need a Dispose(bool)? and if I do, why doesn't Font have it? and since it does not have it, why is FXCop asking me to use it?


Thanks for all the great answers. I think I understand now. Here is

The answer as I see it:

Disposing of "unmanaged" resources falls into two categories:

  • Resources that are wrapped in a managed class (ie Bitmap, Font etc), but still need Dispose to be called to clean them up properly.
  • Resources that you have allocated, which are representations of native resources (ie device contexts that need to be released)
  • Dispose(bool) is used to tell the difference between the two:

  • When Dispose is directly called on your object, you want to free both kinds of "unmanaged" resources.
  • When your object is up for Garbage Collection, you don't need to worry about the first kind of resources. The garbage collector will take care of them when it cleans them up. You only need to worry about true native resources that you have allocated (if any).

  • IDisposable provides a method with the signature

    public void Dispose()
    

    Microsoft best practices (http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx) recommend making a second private method with the signature

    private void Dispose(bool)
    

    Your public Dispose method and Finalizer should call this private Dispose method to prevent disposing managed resources multiple times.

    You can fix the warning you are getting by either implementing IDisposable and disposing of your font object in the dispose method, or creating a Dispose(bool) method in your class, and make your finalizer call that method.


    Dispose(bool)是一种实现FinalizeDispose以清理非托管资源的模式,请参阅此处了解详细信息


    Dispose(bool) is not meant to be public and that is why you don't see it on Font .

    In case some user of your class forgets to call Dispose on your method, you will release the unmanaged resources only by making a call to Dispose(false) in the Finalizer .

    In case IDispose is called correctly, you call the Dispose on managed resources and also take care of the unmanaged.

    The flag is to distinguish the two cases.

    It is a pattern recommended by MSDN.

    链接地址: http://www.djcxy.com/p/54512.html

    上一篇: 为什么类会显式而不是隐式地实现IDisposable?

    下一篇: Dispose vs Dispose(布尔)