COM: Can I call CoUninitialize without calling Release?

I have doubts. I initialize COM, make CoCreateInstance and use some interfaces. Can I call CoUninitialize without calling Release? Does this cause a memory / resource leak?

Thanks, Advance, -Mani.

+4
source share
4 answers

From MSDN:

http://msdn.microsoft.com/en-us/library/ms688715%28VS.85%29.aspx

CoUninitialize should cause the application to shut down, as the last call made to the COM library after the application hides its main windows and enters the main message outline. If there are open conversations left, CoUninitialize starts a modal message loop and sends any pending messages from containers or the server for this COM application. From sending messages, CoUninitialize ensures that the application does not quit before receiving all its pending messages. Non-COM messages are discarded.

You should always call CoUninitialize on shutdown, and by then it doesn't matter if you have a memory leak.

+3
source

Regardless of whether you initialize COM or not, dropping the calls in Release will cause the objects to remain on the server side, it is possible that the entire server was without a reason (if it does not work as a service). In other words, you will have a memory leak on the server side, which can only be fixed by restarting the COM server.

I remember asking similar questions when I first started using COM. The client I was working on used many threads, and I tried to reuse the interfaces for the different tasks that each thread performed. This made managing the interface cache difficult. In the end, there were no shortcuts. If you are not using MTA, GIT, or interface marshaling, the thread that created the interface should also release it.

To simplify the task, try using CComPtr to control the interfaces you create. As with regular pointers, using a smart pointer can sometimes make your life much easier.

+2
source

You should not use CoUninitialize () instead of calling IUnknown::Release() for your objects - these are completely different functions.

IUnknown::Release() simply decrease the reference count for the object and possibly lead to its destruction. If marching is not used, this call is made correctly via vtable (control is transferred directly to the COM server code), and the COM subsystem does not even do anything for this.

CoUninitialize () will free the COM related resource for the calling thread, which I assume is associated with sort related objects. If marching is not used, the objects will remain unreleased, since only your code knows about them.

That is why you should not use one instead of the other.

+1
source

AFAIK, CoUninitialize is "assumed" to free up all COM resources used in the current thread. I would not rely on it. I would prefer that I release everything before calling uninitialise.

0
source

All Articles