Tracking down "COM object that has been separated from its underlying RCW cannot be used."

Add-in Express™ Support Service
That's what is more important than anything else

Tracking down "COM object that has been separated from its underlying RCW cannot be used."
Adam B

Posts: 49
Joined: 2013-12-20

We are having a hard time figuring out what we are doing wrong that is causing a COM object to get re-used after release.

The exception that we get is this:

Exception Source:      ChnlsOutlookAddIn
Exception Type:        System.Runtime.InteropServices.InvalidComObjectException
Exception Message:     COM object that has been separated from its underlying RCW cannot be used.
Exception Target Site: ADXOlFormExplorerSidebar_ADXAfterFormShow

---- Stack Trace ----
       ADXOlFormExplorerSidebar.cs: line 0048, col 13, IL 0013 (0xD)
   AddinExpress.OL.ADXOlForm.AdxOlShow(appWindowProperty As AppWindowProperty, raiseEvents As Boolean)
       AddinExpress.OL.2005.DLL: N 0447 (0x1BF) IL 
   AddinExpress.OL.ADXOlFormsManager.DoExplorerFormShow(Item As ADXOlFormsCollectionItem, ExplorerArgs As ADXOlExplorerArguments)
       AddinExpress.OL.2005.DLL: N 0508 (0x1FC) IL 

The offending code is the following:

private void ADXOlFormExplorerSidebar_ADXAfterFormShow()
    _wbComMain = (WebBrowserCom) webBrowserMain.ActiveXInstance; // LINE 48
     _wbComMain.NewWindow3 += WbComMainNewWindow3;

We have two parts of the code that release COM objects:

        private void ADXOlFormExplorerSidebar_ADXAfterFormHide(object sender, ADXAfterFormHideEventArgs e)
            if (null == _wbComMain) return;
            Marshal.ReleaseComObject(_wbComMain); <<< release _wbComMain when the form hides
            _wbComMain = null;

And this code that was added after the COM release review

 private void WbComMainNewWindow3(ref object ppDisp, ref bool cancel, uint dwFlags, string bstrUrlContext,
            string bstrUrl)
            var uri = new Uri(bstrUrl);
            var description = "Navigating new window: " + uri;
            if (IsOauth(uri))
                var pwf = new WebPopupWindowForm(webBrowserMain, url => GoogleOAuthReturn(url.Fragment))
                    StartPosition = FormStartPosition.CenterParent
                if (null != ppDisp)
                    Marshal.ReleaseComObject(ppDisp); // <<<<<<<<<<<<<<< release ppDisp that is passed in, when replacing with new browser
                ppDisp = pwf.WebBrowserAx;

                Scheduler.Run(description, () =>
                    if (IsNotCurrentLoadIteration())
                        // this isn't the current load itertion, ignore
                }, 1);

Should we not be releasing the browser in either of these cases? Or should we be looking somewhere else to find out what is happening?

Posted 09 Mar, 2015 23:43:16 Top
Andrei Smolin

Add-in Express team

Posts: 17367
Joined: 2006-05-11
Hello Adam,

It looks like the problem is caused by your releasing ppDisp in WbComMainNewWindow3. Could you please explain what you need to achieve? Why do you replace ppdisp with anything else? Also, of what type is webBrowserMain? webBrowserMain.ActiveXInstance? pwf.WebBrowserAx?

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 10 Mar, 2015 06:53:06 Top