I have a native Visual C++ NT service. When the service is started its thread calls CoInitialize()
which attaches the thread to an STA - the service thread uses MSXML through COM interfaces.
When the service receives SERVICE_CONTROL_STOP
it posts a message in the message queue, then later that message is retrieved and the OnStop()
handler is invoked. The handler cleans up stuff and calls CoUnitialize()
. Most of the time it works allright, but once in a while the latter call hangs. I can't reproduce this behavior stably.
I googled for a while and found the following likely explanations:
- failing to release all COM objects owned
- repeatedly calling
CoInitializeEx()
/CoUnitialize()
for attaching to MTA - failing to dispatch messaged in STA threads
The first one is unlikely - the code using MSXML is well tested and analyzed and it uses smart pointers to control objects lifetime, so leaking objects is really unlikely.
The second one doesn't look like the likely reason. I attach to STA and don't call those functions repeatedly.
The third one looks more or less likely. While the thread is processing the message it doesn't run the message loop anymore - it is inside the loop already. I suppose this might be the reason.
Is the latter a likely reason for this problem? What other reasons should I consider? How do I resolve this problem easily?