7

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?

2 Answers 2

17

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

The Dispose implementation is calling the Close().

From Microsoft Docs:

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.

Sign up to request clarification or add additional context in comments.

1 Comment

To confirm what @Shay said about Dispose calling Close, see referencesource.microsoft.com/#System/security/system/security/…
1

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.

4 Comments

why don't they do this in the destructor, that was my question actually. btw where did you get the source code from?
In .Net IDisposable is prefered over destructors.See this: stackoverflow.com/questions/456213/destructor-vs-idisposable I got the code using Ilspy decompiler. ilspy.net
So why this API is not designed with IDisposable do you think?
I can't tell without taking a deeper look at the sources. Even Microsoft code isn't perfect. But for you it's a shorter writing to call Close() than a full using or try/finally block! :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.