在密封类上实现 IDisposable
我认为以前没有人问过这个问题。我对在密封类上实现 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您忘记调用 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 asealed
class. It wouldn't compile at all. Also, it's not recommended to declare newprotected
members insealed
classes.一个小的补充;在一般情况下,常见的模式是使用
Dispose(bool dispose)
方法,以便您知道您是否处于Dispose
(有更多的东西可用)与终结器(您不应该真正接触任何其他连接的托管对象)。例如:
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 inDispose
(where more things are available) vs the finalizer (where you shouldn't really touch any other connected managed objects).For example:
来自 Joe Duffy 的博客:
所以是的,你应该表现得很好。
正如 Mehrdad 提到的,你确实需要终结器。如果您想避免它,您可以查看 安全句柄。我没有足够的 P/Invoke 经验来建议正确的用法。
From Joe Duffy's Weblog:
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.
您不能在密封类中声明虚拟方法。另外,在密封类中声明受保护的成员也会给您带来编译器警告。所以你已经正确地实现了它。
由于显而易见的原因,从终结器中调用 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.