简体   繁体   English

为什么 HashAlgorithm 类要实现 IDisposable?

[英]Why does the HashAlgorithm class implement IDisposable?

While using MD5CryptoServiceProvider I found that it might need to be disposed since it inherits from HashAlgorithm class which implements IDisposable .在使用MD5CryptoServiceProvider ,我发现它可能需要处理,因为它继承自实现IDisposableHashAlgorithm类。 However, the example in the docs didn't dispose it.但是, 文档中的示例没有处理它。

My question is why HashAlgorithm class implements IDisposable ?我的问题是为什么HashAlgorithm类实现IDisposable Isn't hashing just some computations that take place in memory?散列不只是一些发生在内存中的计算吗? What kind of unmanaged resources might be used in hashing?什么样的非托管资源可以用于散列?

You can have a look at sources你可以看看来源

[System.Security.SecuritySafeCritical] // overrides public transparent member
protected override void Dispose(bool disposing)
{
    if (_safeHashHandle != null && !_safeHashHandle.IsClosed)
        _safeHashHandle.Dispose();
    base.Dispose(disposing);
}

It's disposing the internal SafeHashHandle instance, which is used to wrap the unmanaged resource (operation system handle) and calls Dispose from base HashAlgorithm class.它正在处理内部SafeHashHandle实例,该实例用于包装非托管资源(操作系统句柄)并从基HashAlgorithm类调用Dispose You have to properly dispose and release this handle after usage您必须在使用后妥善处理并释放此手柄

[System.Security.SecurityCritical]
protected override bool ReleaseHandle()
{
    FreeHash(handle);
    return true;
}

This method is override of abstract ReleaseHandle() method from base SafeHandle class.此方法覆盖了基SafeHandle类的抽象ReleaseHandle()方法。 You can read more about this class at MSDN , basically this class is a wrapper to any operation system resource您可以在MSDN上阅读有关此类的更多信息,基本上此类是任何操作系统资源的包装器

It contains a critical finalizer that ensures that the handle is closed and is guaranteed to run during unexpected AppDomain unloads, even in cases when the platform invoke call is assumed to be in a corrupted state.它包含一个关键的终结器,可确保关闭句柄并保证在意外的 AppDomain 卸载期间运行,即使在假定平台调用处于损坏状态的情况下也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM