是否应该对没有终结器的对象调用 GC.SuppressFinalize?

发布于 2024-07-14 06:19:22 字数 218 浏览 7 评论 0原文

由于某种原因 FXCop 似乎认为 我应该调用 GC.SuppressFinalize在 Dispose 中,无论我是否有终结器。

我错过了什么吗? 是否有理由对未定义终结器的对象调用 GC.SuppressFinalize?

For some reason FXCop seems to think I should be calling GC.SuppressFinalize in Dispose, regardless of whether I have a finalizer or not.

Am I missing something? Is there a reason to call GC.SuppressFinalize on objects that have no finalizer defined?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ゃ人海孤独症 2024-07-21 06:19:22

不需要在 Dispose 中调用 GC.SuppressFinalize(this),除非:

  • 您是实现用于重写的虚拟 Dispose 方法的基类(同样,即使在这里,这也可能不是您的责任,但是您在这种情况下可能想要这样做)
  • 你自己有一个终结器。 从技术上讲,.NET 中的每个类都有一个终结器,但如果唯一存在的终结器是 Object 中的终结器,则该对象不会被视为需要终结,并且不会在 GC 时放入终结列表中

我想说,假设您没有上述任何情况,您可以安全地忽略该消息。

There's no need to call GC.SuppressFinalize(this) in Dispose, unless:

  • You are the base class that implements virtual Dispose methods intended for overriding (again, it might not be your responsibility even here, but you might want to do it in that case)
  • You have a finalizer yourself. Technically, every class in .NET has a finalizer, but if the only finalizer present is the one in Object, then the object is not considered to need finalizing and isn't put on the finalization list upon GC

I would say, assuming you don't have any of the above cases, that you can safely ignore that message.

行至春深 2024-07-21 06:19:22

IL 中总是有一个终结器 - System.Object.Finalize() 存在于每个类中,因此如果您创建一个自定义类,它就会有一个您想要抑制的终结器。 话虽如此,并非所有对象都会放入终结队列中,因此,如果您实现自己的终结器,则从技术上讲,您只需要抑制终结。

如果您要实现 IDisposable 来包装非托管资源,则应包含一个终结器,并且应阻止其运行,因为理论上您在 Dispose 被调用。

There is always a finalizer in IL - System.Object.Finalize() exists in every class, so if you make a custom class, it has a finalizer you want to suppress. That being said, not all objects are put on the finalization queue, so you only techncially should need to suppress finalization if you implement your own finalizer.

If you're implementing IDisposable to wrap unmanaged resources, you should include a finalizer, and you should prevent this from running, since in theory you're doing the cleanup already when Dispose is called.

情绪少女 2024-07-21 06:19:22

看起来 FxCop 只是检查 Dispose(),而不检查析构函数是否存在。

忽略它应该是安全的。

It looks like FxCop simply inspects the Dispose() and doesn't check for the presence of a destructor.

It should be safe to ignore.

埋情葬爱 2024-07-21 06:19:22

所有对象都有一个终结方法,即使您没有使用 ac# 析构函数(实际上并不能保证由 GC 调用)实现该方法。 如果您已经实现了 IDisposable,那么最好抑制该调用,因为这意味着您已决定显式执行终结。

devx 文章

All objects have a finalizer method, even if you have not implemented one by using a c# destructor (which is not actually guaranteed to be called by the GC). It's just good practice to supress the call if you have implemented IDisposable because that means you have decided to perform the finalization explictly.

devx article

你对谁都笑 2024-07-21 06:19:22

如果没有定义终结器,我认为没有必要调用 SuppressFinalize() 。 如果您想采取防御措施,那么拥有终结器和 Dispose() 会很好,这样您就不需要依赖客户端始终调用 Dispose()。 这样当他们忘记时你就不会泄漏资源。

I don't see any need to call SuppressFinalize() if there's no finalizer defined. If you want to be defensive then it can be good to have a finalizer as well as Dispose(), so you don't need to rely on clients to always call Dispose(). Then you won't leak resources when they forget.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文