If you ever develop COM add-ins for the VBA editor (or for some Office application), you may find that sometimes you need to call Marshall.ReleaseComObject (when you get strange errors that disappear when calling Marshall.ReleaseComObject) and most of the time (by default) you don’t have to (when you get an InvalidComObjectException). The issue is a bit tricky because of a number of factors, such as:
– The RCWs are reused and cached per-AppDomain
– An AppDomain can contain more than one .NET add-in
– RCWs have a reference counter, and the COM-object that the RCW wraps has another reference counter.
– There can be COM singletons involved.
– Etc.
Here you have some useful links to get a better knowledge:
Marshal.ReleaseComObject Method
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.releasecomobject.aspx
Marshal.ReleaseComObject Considered Dangerous
http://blogs.msdn.com/b/visualstudio/archive/2010/03/01/marshal-releasecomobject-considered-dangerous.aspx
The mapping between interface pointers and runtime callable wrappers (RCWs)
http://blogs.msdn.com/b/mbend/archive/2007/04/18/the-mapping-between-interface-pointers-and-runtime-callable-wrappers-rcws.aspx
Chapter 2: Basics of Office Interoperability (Part 2 of 3)
http://msdn.microsoft.com/en-us/library/office/aa679807%28v=office.11%29.aspx