IClassFactory::LockServer
The other interesting member function of a class factory is LockServer. Here the server increments of decrements a lock count depending on the fLock parameter. If the last lock is removed and there are no objects in server, the server initiates unloading which again, is specific to the type of server and a topic for a later section. In any case, COM does not define a standard method for tracking the lock count. Since other code outside of the class factory may need access to the lock count, a global variable works well:
//Global server lock count.
ULONG g_cLock=0;
The implementation of LockServer is correspondingly simple:
HRESULT CTextRenderFactory::LockServer(BOOL fLock)
{
if (fLock)
g_cLock++;
else
{
g_cLock--;
[Initiate unloading if there are no objects and no locks]
}
return NOERROR;
}
It is perfectly reasonable to double the use of g_cObj for counting locks as well as objects. You might want to keep them separate for debugging purposes.