I make calls to CoInitializeEx, specifically:
CoInitializeEx(NULL, COINIT_DISABLE_OLE1DDE | COINIT_APARTMENTTHREADED);
In threads that my application creates, but then terminates before the application exits. Is it important to pair these with CoUninitialize calls, or do those resources get freed when the threads terminate?
preguntado el 08 de noviembre de 11 a las 17:11
Resources are usually returned to the system when the process terminates, not when one of its threads does.
Sin embargo, CoUninitialize() does not only free resources and unload DLLs, it also enters a modal message loop in order to pump the remaining COM messages before the thread terminates. The documentation says:
If there are open conversations remaining,
CoUninitializestarts a modal message loop and dispatches any pending messages from the containers or server for this COM application. By dispatching the messages,
CoUninitializeensures that the application does not quit before receiving all of its pending messages. Non-COM messages are discarded.
Therefore, in order to avoid RPC errors on the callers' side, I'd recommend you follow the documentation's advice and always call
CoUninitialize() before terminating threads that have called