windows – 在我的应用程序退出之前终止的线程上调用CoUninitialize是否很重要?

我打电话给CoInitializeEx,具体来说:

CoInitializeEx(NULL, COINIT_DISABLE_OLE1DDE | COINIT_APARTMENTTHREADED);

在我的应用程序创建的线程中,但随后在应用程序退出之前终止.将这些与CoUninitialize调用配对是否很重要,或者在线程终止时是否释放这些资源?

当进程终止时,通常会将资源返回给系统,而不是在其中一个线程执行时.

但是,CoUninitialize()不仅释放资源并卸载DLL,它还会进入模态消息循环,以便在线程终止之前抽取剩余的COM消息.文件说:

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.

因此,为了避免调用方的RPC错误,我建议您遵循文档的建议,并在终止调用CoInitializeEx()的线程之前始终调用CoUninitialize().

翻译自:https://stackoverflow.com/questions/8054487/is-it-important-to-call-couninitialize-on-a-thread-that-will-be-terminated-befor

转载注明原文:windows – 在我的应用程序退出之前终止的线程上调用CoUninitialize是否很重要?