Closing a Certificate Store

CertCloseStore closes a certificate store.

When a certificate store is closed, each of the certificate contexts in that store has its reference count reduced by one. Memory is automatically freed for certificates whose reference count goes to zero.

Setting CERT_CLOSE_STORE_FORCE_FLAG with CertCloseStore closes the certificate store and frees all of its certificate contexts regardless of their reference count. In some cases, such as in multithreaded programs, this may not be desirable. If CERT_CLOSE_STORE_CHECK_FLAG is set, the store is closed, but a warning value is returned by the function if memory is still allocated for certificates whose reference counts have not been reduced to zero. If a certificates reference count is greater than zero, one or more duplicates of that certificate contexts have not been freed. Use CertFreeCertificateContext, CertFreeCRLContext, and CertFreeCTLContext to free any certificates left open.

Note  A certificate context is a data structure of type CERT_CONTEXT that has, among other members, a pointer to the encoded certificate blob and a pointer to a CERT_INFO data structure. The CERT_INFO structure contains the most significant certificate data. For more information about certificate, CRL and CTL context data structures, see Certificate Encoding/Decoding.

Each certificate context also contains a reference count indicating the number of copies of the context's address that have been assigned. Each time a certificate context is duplicated, its reference count in incremented by one. Each time a pointer to a certificate context is freed, the reference count in the certificate context is decremented by one. When the reference count on a certificate context reaches zero, the memory holding the context is de-allocated. Memory allocated for a certificate context is also de-allocated when that context is in a store and the store is closed using CERT_CLOSE_STORE_FORCE_FLAG. If the memory for a context is de-allocated and pointers to that context are still in use, those pointers become invalid.