What is the implication of not using X509Store.Close method?

旧时模样 提交于 2019-12-23 18:31:12

问题


If I keep initializing X509Store certificate stores and don't use their Close() method, what is the implication of this?

In the code example given in documentation, they don't use try..finally block to make a call to Close method. If this certificate store is something that needs to be freed, why does not the API of the class designed to derive from IDisposable or why does not this class have a implicit destructor called when object goes out of scope?


回答1:


In .NET 4.6, X509Store was modified and now implementing IDisposable.

The Dispose implementation is calling the Close().

From MSDN:

Starting with the .NET Framework 4.6, this type implements the IDisposable interface. When you have finished using the type, you should dispose of it either directly or indirectly.




回答2:


Internally, the Close method release the handle pointing to the unmanaged object.

public void Close()
{
    if ((this.m_safeCertStoreHandle != null) && !this.m_safeCertStoreHandle.IsClosed)
    {
        this.m_safeCertStoreHandle.Dispose();
    }
}

I'd rather call the Close method to avoid memory leaks.



来源:https://stackoverflow.com/questions/18475558/what-is-the-implication-of-not-using-x509store-close-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!