?Â?Ð?ìCOM object that has been separated from its underlying RCW cannot be used?Â?Ð?í when accessing HTMLBody

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

?Â?Ð?ìCOM object that has been separated from its underlying RCW cannot be used?Â?Ð?í when accessing HTMLBody
 
Thomas Darde




Posts: 14
Joined: 2016-07-05
Dear support,

I am currently developing an VSTO Outlook Addin to add a signature when an email is opened.
In one computer, I am getting this error when I am trying to access the HTMLBody property.

Apparently, this error means that the com object MailItem has already been released. But this should not be the case as I do not invoke Marshal.ReleaseComObject on this object.


Here is the error:

System.StubHelpers.StubHelpers.GetCOMIPFromRCW(objSrc As Object, pCPCMD As IntPtr, ppTarget As IntPtr&, pfNeedsRelease As Boolean&)
mscorlib.dll: N 00000 (0x0) JIT
Microsoft.Office.Interop.Outlook._MailItem.set_HTMLBody(HTMLBody As String)
mscorlib.dll: N 00000 (0x0) JIT
SigiliumForOutlook.Signature.SignatureService.AddHtmlSignature(mailItem As MailItem, sender As String)
mscorlib.dll: N 0311 (0x137) IL
SigiliumForOutlook.Signature.SignatureService.AddHtmlSignatureOpenEvent(mailItem As MailItem)
mscorlib.dll: N 0288 (0x120) IL

FYI, I am using HTMLAgilityPack to manipulate mailItem.HTMLBody.

Does it exist any things to take car when accessing mailItem.HTMLBody? Do you have any ideas to help me?
Thanks a lot for your help
Posted 07 Sep, 2016 05:41:31 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Hello Thomas,

Thomas Darde writes:
I am currently developing an VSTO Outlook Addin


Do you develop a VSTO add-in? Is this correct? Or, you develop an Add-in Express add-in? Below I assume that you use Add-in Express. Please confirm.

If you use an Outlook Item (not Items!) Events class in your project, then calling RemoveConnection will release the item (which is the source of events) if the second parameter of the ConnectTo method is true. Is this the case?

You might also call Marshal.ReleaseComObject in this scenario:

object item = anInspector.CurrentItem;
Outlook.MailItem mail = item as Outlook.MailItem;
// use mail here
Marshal.ReleaseComObject(item); // this also releases mail



Andrei Smolin
Add-in Express Team Leader
Posted 07 Sep, 2016 06:40:24 Top
Thomas Darde




Posts: 14
Joined: 2016-07-05
Hi Andrei,

Yes you are right we are using Add-in Express add-in.
So far, we are using the method ConnectTo with the parameter true that means that the call to the method RemoveConnection will release the object.


        private void adxOutlookAppEvents1_InspectorActivate(object sender, object inspector, string folderName)
        {
            LogHelper.Instance.Log("Inspector activate", Severity.Info);

            if (canConnect)
            {
                Outlook._Inspector olInsp = (Outlook._Inspector)inspector;
                object item = olInsp.CurrentItem;
                if (item is Outlook._MailItem)
                {
                    LogHelper.Instance.Log("Inspector activate it is mailitem", Severity.Info);
                    outlookItemEvents.ConnectTo(item, true);
                }
            }
        }


And the method RemoveConnection is called only once:


        private void AddinModule_AddinBeginShutdown(object sender, EventArgs e)
        {
            if (outlookItemEvents != null)
            {
                outlookItemEvents.RemoveConnection();
                outlookItemEvents.Dispose();
            }
        }


Does it mean that we never release the mailItem properly ?
When do we have to call outlookItemEvents.RemoveConnection() to be safe?

Thanks a lot for your help
Posted 07 Sep, 2016 07:22:13 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
I would disconnect from events of the item connected before connecting to events of another item.

But this doesn't explain the exception that you get.

Consider this scenario. You get the item in an Add-in Express such as ItemSend, ExplorerInlineResponse, etc.. You cache the item outside of the event handler and use the item after the event handler completes. You will get the exception in this situation because Add-in Express releases all COM objects passed to an event handler right after the event handler finishes.


Andrei Smolin
Add-in Express Team Leader
Posted 07 Sep, 2016 07:49:18 Top
Thomas Darde




Posts: 14
Joined: 2016-07-05
I have checked the code again and again and apparently we do not have any objects that are stored outside of the event handler and that are reused later.

FYI, I have updated my code to follow this project: https://www.add-in-express.com/files/howtos/cs/adx_ol_cs_ProcessItemEvents.zip

Do you have any suggestions that could explain this error?
Thanks a lot
Posted 08 Sep, 2016 00:24:41 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Hello Thomas,

I've checked that example and found that it releases the item in the ItemSend event (IOW when you send an item). Is this the cause of the issue that you experience?


Andrei Smolin
Add-in Express Team Leader
Posted 08 Sep, 2016 05:12:52 Top