Calling Marshall.ReleaseComObject

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

Marshal.ReleaseComObject Considered Dangerous

The mapping between interface pointers and runtime callable wrappers (RCWs)

Chapter 2: Basics of Office Interoperability (Part 2 of 3)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.