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?
Resources are usually returned to the system when the process terminates, not when one of its threads does.
However, 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,
CoUninitialize
starts a modal message loop and dispatches any pending messages from the containers or server for this COM application. By dispatching the messages,CoUninitialize
ensures 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 CoInitializeEx()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With