在密封类上实现 IDisposable

发布于 2024-08-04 20:04:24 字数 1025 浏览 5 评论 0原文

我认为以前没有人问过这个问题。我对在密封类上实现 IDisposable 的最佳方法有点困惑——具体来说,是一个不从基类继承的密封类。 (即“纯密封类”,这是我编造的术语。)

也许你们中的一些人同意我的观点,即实现 IDisposable 的准则非常令人困惑。也就是说,我想知道我打算实现 IDisposable 的方式是否足够且安全。

我正在编写一些通过 Marshal.AllocHGlobal 分配 IntPtr 的 P/Invoke 代码,自然地,我想干净地处理我创建的非托管内存。所以我正在考虑类似的事情,

using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
public sealed class MemBlock : IDisposable
{
     IntPtr ptr;
     int length;

     MemBlock(int size)
     {
           ptr = Marshal.AllocHGlobal(size);
           length = size;
     }

     public void Dispose()
     {
          if (ptr != IntPtr.Zero)
          {
               Marshal.FreeHGlobal(ptr);
               ptr = IntPtr.Zero;
               GC.SuppressFinalize(this);
          }
     }

     ~MemBlock()
     {
           Dispose();
     }    
}

我假设因为 MemBlock 是完全密封的,并且永远不会从实现 virtual protected Dispose(bool dispose) 的另一个类派生没有必要。

另外,终结器是绝对必要的吗?欢迎所有想法。

I don't think this question has been asked before. I'm a bit confused on the best way to implement IDisposable on a sealed class—specifically, a sealed class that does not inherit from a base class. (That is, a "pure sealed class" which is my made up term.)

Perhaps some of you agree with me in that the guidelines for implementing IDisposable are very confusing. That said, I want to know that the way I intend to implement IDisposable is sufficient and safe.

I'm doing some P/Invoke code that allocates an IntPtr through Marshal.AllocHGlobal and naturally, I want to cleanly dispose of the unmanaged memory I've created. So I'm thinking of something like this

using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
public sealed class MemBlock : IDisposable
{
     IntPtr ptr;
     int length;

     MemBlock(int size)
     {
           ptr = Marshal.AllocHGlobal(size);
           length = size;
     }

     public void Dispose()
     {
          if (ptr != IntPtr.Zero)
          {
               Marshal.FreeHGlobal(ptr);
               ptr = IntPtr.Zero;
               GC.SuppressFinalize(this);
          }
     }

     ~MemBlock()
     {
           Dispose();
     }    
}

I'm assuming that because MemBlock is completely sealed and never derives from another class that implementing a virtual protected Dispose(bool disposing) is not necessary.

Also, is the finalizer strictly necessary? All thoughts welcome.

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

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

发布评论

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

评论(4

游魂 2024-08-11 20:04:24

如果您忘记调用 Dispose,则终结器作为后备机制是必要的,以便最终释放非托管资源。

不,您不应该在密封类中声明虚拟方法。它根本无法编译。另外,不建议在 sealed 类中声明新的 protected 成员。

The finalizer is necessary as a fallback mechanism to eventually free unmanaged resources if you forgot to call Dispose.

No, you shouldn't declare a virtual method in a sealed class. It wouldn't compile at all. Also, it's not recommended to declare new protected members in sealed classes.

蛮可爱 2024-08-11 20:04:24

一个小的补充;在一般情况下,常见的模式是使用Dispose(bool dispose)方法,以便您知道您是否处于Dispose (有更多的东西可用)与终结器(您不应该真正接触任何其他连接的托管对象)。

例如:

 public void Dispose() { Dispose(true); }
 ~MemBlock() { Dispose(false); }
 void Dispose(bool disposing) { // would be protected virtual if not sealed 
     if(disposing) { // only run this logic when Dispose is called
         GC.SuppressFinalize(this);
         // and anything else that touches managed objects
     }
     if (ptr != IntPtr.Zero) {
          Marshal.FreeHGlobal(ptr);
          ptr = IntPtr.Zero;
     }
 }

A minor addition; in the general case, a common pattern is to have a Dispose(bool disposing) method, so that you know whether you are in Dispose (where more things are available) vs the finalizer (where you shouldn't really touch any other connected managed objects).

For example:

 public void Dispose() { Dispose(true); }
 ~MemBlock() { Dispose(false); }
 void Dispose(bool disposing) { // would be protected virtual if not sealed 
     if(disposing) { // only run this logic when Dispose is called
         GC.SuppressFinalize(this);
         // and anything else that touches managed objects
     }
     if (ptr != IntPtr.Zero) {
          Marshal.FreeHGlobal(ptr);
          ptr = IntPtr.Zero;
     }
 }
梦开始←不甜 2024-08-11 20:04:24

来自 Joe Duffy 的博客

对于密封类,此模式需要
不被跟踪,这意味着您应该
只需实现您的 Finalizer 并
用简单的方法处理(即
~T()(C# 中的 Finalize)和 Dispose())。
当选择后一条路线时,您的
代码仍应遵守
以下指南涉及
实施最终确定和
处理逻辑。

所以是的,你应该表现得很好。

正如 Mehrdad 提到的,你确实需要终结器。如果您想避免它,您可以查看 安全句柄。我没有足够的 P/Invoke 经验来建议正确的用法。

From Joe Duffy's Weblog:

For sealed classes, this pattern need
not be followed, meaning you should
simply implement your Finalizer and
Dispose with the simple methods (i.e.
~T() (Finalize) and Dispose() in C#).
When choosing the latter route, your
code should still adhere to the
guidelines below regarding
implementation of finalization and
dispose logic.

So yes, you should be good.

You do need the finalizer as Mehrdad mentioned. If you want to avoid it, you might take a look at SafeHandle. I don't have enough experience with P/Invoke to suggest the correct usage.

终陌 2024-08-11 20:04:24

您不能在密封类中声明虚拟方法。另外,在密封类中声明受保护的成员也会给您带来编译器警告。所以你已经正确地实现了它。
由于显而易见的原因,从终结器中调用 GC.SuppressFinalize(this) 是不必要的,但它不会造成损害。

在处理非托管资源时,拥有终结器至关重要,因为它们不会自动释放,您必须在对象被垃圾收集后自动调用终结器中执行此操作。

You cannot declare virtual methods in a sealed class. Also declaring protected members in a sealed class gives you a compiler warning. So you've implemented it correctly.
Calling GC.SuppressFinalize(this) from within the finalizer is not necessary for obvious reasons but it cannot harm.

Having a finalizer is essential when dealing with unmanaged resources, because they are not freed automatically, you have to do it in the finalizer with is called automatically after the object has been garbage collected.

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