处置与处置(布尔) [英] Dispose vs Dispose(bool)

查看:92
本文介绍了处置与处置(布尔)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对处置感到困惑。我正在尝试让我的代码正确处理资源。因此,我一直将我的类设置为 IDisposable (使用 Dispose 方法),以确保 Dispose 方法被调用。


但是现在FXCop告诉我很多有关 dispose = false 并调用 Dispose(false)


我看不到 Dispose 需要一个布尔值的方法。我需要做一个吗?如果是这样,为什么?为什么不只是拥有一个在处理时被调用的方法呢?


我在这里看到了一些代码: CA1063:正确实现IDisposable-Microsoft文档,该文档演示了如何进行处置需要 bool 的方法。它说这是针​​对本地资源还是托管资源。 但是我认为处理的全部目的仅是针对非托管资源。


此外,FXCop抱怨的内容是:

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

它说:


CA1063:Microsoft.Design:修改 OwnerDrawnPanel。〜OwnerDrawnPanel(),以便调用 Dispose(false)然后返回。


但是 Font 上没有(我可以找到) Dispose(bool)。 / p>

总结一下:


我为什么需要 Dispose(bool)?如果可以,为什么字体没有呢?而且由于它没有它,为什么FXCop要求我使用它?




感谢所有出色的回答。我想我现在明白了。这是


我所看到的答案:


处理非托管邮件资源分为两类:


  1. 包装在托管类中的资源(即 Bitmap Font 等),但仍需要调用 Dispose 才能正确清理它们。

  2. 您已分配的资源,它们代表本机资源(即,需要释放的设备上下文)

Dispose(bool)用于区分两者之间的区别:


  1. 何时 Dispose 直接在您的对象上调用,您想释放两种非托管对象。

  2. 当对象用于垃圾收集时,您无需担心第一种资源。垃圾收集器在清理它们时会照顾它们。您只需要担心已分配的真实本机资源(如果有)。


解决方案

IDisposable 提供了带有签名的方法

  public void Dispose() 

Microsoft最佳做法(实施Dispose方法)建议制作第二个带有签名的私有方法

 私人作废Dispose(bool)

您的公众 Dispose 方法和终结器应调用此私有的 Dispose 方法,以防止多次处置托管资源。



您可以通过实现 IDisposable 并在dispose方法中处理字体对象来解决警告,也可以创建 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: CA1063: Implement IDisposable correctly - Microsoft Docs that shows how to make a Dispose 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:

  1. Resources that are wrapped in a managed class (ie Bitmap, Font etc), but still need Dispose to be called to clean them up properly.
  2. 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:

  1. When Dispose is directly called on your object, you want to free both kinds of "unmanaged" resources.
  2. 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 (Implement a Dispose method) 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.

这篇关于处置与处置(布尔)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆