COM's local/remote transparency isolates the location and activation of a server from the client.
A client simply asks for a class factory and, using
IClassFactory::CreateInstance, creates instances of a COM object. Recall that COM uses reference counting to manage the lifetime of a COM object. When a COM object, via
QueryInterface, "gives away" an interface pointer (i.e., assigns the output pointer parameter to an interface), it increments a reference counter by calling
The COM client calls
Release when it is finished using an interface pointer.
Release decrements the reference counter.
Reference counting supports lifetime management of the COM object. A COM server uses reference counts to track active (COM) objects.
A server can be unloaded from memory when all its reference counts are 0.
In-process servers provide function
to assist with the unloading process.
Periodically, COM will call into
to ask the server if it can be removed from memory.
a server checks all its reference counts and server locks (i.e., set via
If they are all 0, no COM objects within the server are in use. The server returns
Upon seeing a non-zero return, COM will unload the in-process server.
If any of the reference counts are non-zero or the lock counter is non-zero, the server returns
and COM leaves the server in memory.