¿Es importante llamar a CoUninitialize en un hilo que se terminará antes de que salga mi aplicación?

I make calls to CoInitializeEx, specifically:


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

Even if it is not important to do this, why wouldn't you pair them? -

Because the code is already in testing, and I'd rather not change it if the change is irrelevant, but point taken. -

1 Respuestas

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, 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().

respondido 08 nov., 11:21

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.