Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Release COM Components

Is it really necessary to release COM components from Office PIA, when you don't need them anymore by invoking Marshal.ReleaseComObject(..)?

I found various and contradictory advices on this topic on the web. In my opinion, since Outlook PIA is always returning a new references to its interfaces as returning values from its methods, it is not necessary to explicitly release it. Am I right?

like image 782
Nenad Dobrilovic Avatar asked Oct 03 '08 13:10

Nenad Dobrilovic


1 Answers

With Microsoft Office, in general, you do need to explicitly release your references, which can be safely done in two stages:

(1) First release all the minor object to which you do not hold a named object variable via a call to GC.Collect() and then GC.WaitForPendingFinalizers(). (You need to call this twice, if the objects involved could have finalizers, such as when using Visual Studio Tools for Office (VSTO).)

(2) Then explicitly release the objects to which you hold a named variable via a call to Marshall.FinalReleaseComObject() on each object.

That's it. :-)

I discussed this in more detail in a previous post, along with a code example.

like image 74
Mike Rosenblum Avatar answered Sep 25 '22 02:09

Mike Rosenblum