I'm facing the following issue
COM object that has been separated from its underlying RCW cannot be used.'
System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.
at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease)
at Microsoft.Office.Interop.Outlook._MailItem.get_Subject()
The following code:
var mail = inspectorWrapper.MailItem;
showcaseModel.Subject = mail?.Subject;
showcaseModel.Title = mail?.Subject;
How I can avoid that exception?
Should I release the mail object with Marshal.ReleaseComObject(mail);
?
That is a good indicator that you need to review your add-in's code and check all
Marshal.ReleaseComObjectstatements.If you don't have such statements in the code of your add-in, that indicates that you are trying to use unavailable object from the OOM in the code. For example, a new inspector window is opened where you retrieve an object reference to the mail item and keep it in the code. But users can send such items, so your object reference is getting detached from the underlying COM object. It simply is not valid any longer - the item was moved to another folder (or deleted, if the
DeleteAfterSubmitproperty is set on the item).The recommendation is the same - review your source code.