问题
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