Each COM interface that is returned from the CCW is reference counted in the usual manner. The CCW in turn maintains a traced reference the managed object. That way the CCW can ensure that that the underlying managed object remains alive as long as unmanaged references exist on the wrapper. It also allows other managed objects to maintain a reference to the same managed object as the CCW.
However, unlike COM objects, managed object do not have deterministic lifetimes. That means that even though all reference to a CCW have been released, it may be some time before the managed object it wraps is actually freed. COM clients that make assumptions about when the objects they reference will be delete may experience unexpected behavior.
Under some circumstance, leaks can also occur when an unmanaged process is shut down. Image an unmanaged process that holds a reference on a managed object implemented in another DLL. A CCW is created to wrap the managed object being referenced. Once the reference is released, the CCW becomes eligible for collection. If the process is shut down before the collection takes place, the object will leak. While you might think that the runtime could perform a collection before it shuts down (within DLLMain for example), there are several reasons why that isn’t possible.
To ensure a leak-free shut down under these circumstances the unmanaged process must explicitly call CoEEShutDown. By explicitly calling CoEEShutDown, the runtime is given an opportunity to perform one last collection before the process is destroyed. However, CoEEShutDown collects all outstanding objects in any order. There can be no assumptions about the order in which objects are destroyed.
To ensure a leak-free shut down under these circumstances the unmanaged process must be sure to call CoUninitialize(). By explicitly calling CoUninitialize(), the runtime is given an opportunity to perform one last collection before the process is destroyed. However, CoUninitialize() collects all outstanding objects in any order. There can be no assumptions about the order in which objects are destroyed. This technique applies only on Win2000.