In a Microsoft Office AddIn we are passed COM objects in events. To take a specific case, when Word opens a document we are called and passed a Document object. So when do we need to call Marshal.ReleaseComObject()?
- If we access the Document object do we need to call release on it? Or can we assume Word has already accessed it and will clean it up?
- If we access Document.Name that gives us a string. As a string is not a COM object we do not need to clean that up - correct?
- But if we access any member that returns a class that is wrapping a COM object (which is any class returned by a member method/function), we do need to call release on that - correct?
And what happens if we miss a release? Any COM object we hold onto for an indeterminate amount of time we wrap in a class of ours with IDisposable implemented. We call Dispose() when we're done. But some of the code handling this is complex and I'm guessing we have an occasional case where we're not calling Dispose.
Are we better off having a finalizer which then adds overhead for every single instance of these objects (a lot!)? Or are we better off with a small number of Word COM objects that are never released?
thanks - dave