underlying object released exception when handling reply events in inspector

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

underlying object released exception when handling reply events in inspector
 
Niels Ziegler




Posts: 49
Joined: 2017-02-03
I used an example from the ADX blog: How to handle Outlook item’s Reply event

I am connecting to the item and handling reply/replayAll and forward event to store some information extracted from the item that is replied to.

In the ReplyAllChecker class, I override ProcessReply and then call a method, that accesses the ADXOutlookItemEvents.ItemObj object. This is in COM-Released state, if my call comes from an inspector window. It works fine from the explorer.
Posted 08 Aug, 2017 04:38:59 Top
Niels Ziegler




Posts: 49
Joined: 2017-02-03
Here's the exception I get, and it is thrown when I try to access the item type (is MailItem)


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

---- Stack Trace ----
OutlookMessageWrapper.OutlookMessageItemFactory.GetMessageItem(message As Object)
OutlookMessageItemFactory.cs: line 0020, col 13, IL 0003 (0x3)
RuSClassificationAddin.Events.OutlookItemEventsClass.TransferClassificationAttribute(response As Object, e As ADXCancelEventArgs)
HandleMailReply.cs: line 0155, col 24, IL 0019 (0x13)
RuSClassificationAddin.Events.OutlookItemEventsClass.ProcessForward(forward As Object, e As ADXCancelEventArgs)
HandleMailReply.cs: line 0055, col 13, IL 0001 (0x1)
AddinExpress.MSO.ADXOlItemEvents_10_SinkHelper.AddinExpress.MSO.IOutlookItemEvents_10.Forward(forward As Object, cancel As Boolean&)
OutlookMessageWrapper.DLL: N 0006 (0x6) IL
Posted 08 Aug, 2017 05:09:31 Top
Niels Ziegler




Posts: 49
Joined: 2017-02-03
Here's some additional info I collected:
Before the reply inspector opens, I am using the event to store some information from the "old" item. When I comment out that method, the problem occurs once, then not on subsequent reply/forward actions.

The events go as follows:

private void adxCommand_Forward_OnAction(object sender, IRibbonControl control, bool pressed, ADXCancelEventArgs e)


Where I call a method to get the current item, which I release after I got the header information.

object currentItem = AddInHelpers.GetCurrentItem(OutlookApp.ActiveInspector(), OutlookApp.ActiveExplorer());


Next event is:

 public override void ProcessForward(object forward, AddinExpress.MSO.ADXCancelEventArgs e)


where I try to access ItemObj and then it fails.
Posted 08 Aug, 2017 05:20:09 Top
Dmitry Kostochko


Add-in Express team


Posts: 2731
Joined: 2004-04-05
Hi Niels,

I have just tested the sample's code and found that this.ItemObj in the following method contains a valid COM object:

public override void ProcessReply(object response, AddinExpress.MSO.ADXCancelEventArgs e)
{
    Debug.Print("The  Reply event of the item with the subject " + getSubject() + " has occurred.");
    if (this.ItemObj is Outlook.MailItem)
    {
        Outlook.MailItem mail = this.ItemObj as Outlook.MailItem;
        if (mail != null)
        {
            Outlook.Recipients recipients = mail.Recipients;
            if (recipients.Count > 1)
            {
                if (MessageBox.Show("Did you mean to click 'Reply All' ?", this.Module.AddinName + " sample project", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Debug.Print("The Reply action is cancelled by the user.");
                    e.Cancel = true;
                }
            }
            Marshal.ReleaseComObject(recipients);
        }
    }
}


I used Outlook 2016 32-bit for testing, I opened an email in the Inspector window and clicked the Reply button on the ribbon. Please provide more details about the issue on your side.
Posted 08 Aug, 2017 05:22:10 Top
Dmitry Kostochko


Add-in Express team


Posts: 2731
Joined: 2004-04-05
Hi Niels,

Thank you for the additional information. Could you please send me your project (or even better a small demo project that reproduces the issue) for testing? I will try to determine the cause of the issue.
Posted 08 Aug, 2017 06:03:04 Top
Niels Ziegler




Posts: 49
Joined: 2017-02-03
Hi Dmitry Kostochko,

Creating a demo is of course time consuming, since there are many events going on now. Can I send the project to you through a private channel?

There is obviously a difference between using the reply in explorer mode or in inspector mode, since it works in explorer.
I also noticed, that the COM exception does not occur, if I do not access the item, just before I process the event in the "replyAllChecker" from the example project. Since I am trying to transfer some information, I have to get the current item from explorer, use the PropertyAccessor to read header information, then release the object.

Just after that, in the ProcessReply/Forward event I try to get the object again and fail. If I close the reply, then try again, the exception does not occur anymore, even though the events stay the same from my point of view in debugger.

So the events are:

1)open mail in inspector
2)click reply
3)handle adxCommand_Reply_OnAction event
4)retrieve, read then dispose current inspector item
5)handle ProcessReply in "OutlookItemEvents" (replyAllChecker)
6)get COM exception because ItemObj is released

close reply window
click reply again
3)handle adxCommand_Reply_OnAction event
4)retrieve, read then dispose current inspector item
5)handle ProcessReply in "OutlookItemEvents"
6)no exception
Posted 09 Aug, 2017 12:57:33 Top
Dmitry Kostochko


Add-in Express team


Posts: 2731
Joined: 2004-04-05
Hi Niels,

Okay, please send me your main project for testing. You can find our support email address in the readme.txt file located in the Add-in Express installation folder.
Posted 10 Aug, 2017 04:39:16 Top
Niels Ziegler




Posts: 49
Joined: 2017-02-03
I have sent the project to the support mail adress today.
Posted 10 Aug, 2017 10:09:54 Top
Dmitry Kostochko


Add-in Express team


Posts: 2731
Joined: 2004-04-05
Hi Niels,

I have received it, thank you. You have quite a lot of code, it will take some time to investigate the issue...
Posted 10 Aug, 2017 10:27:30 Top
Niels Ziegler




Posts: 49
Joined: 2017-02-03
The solution you proposed seems to have solved the issue.

I was on the right track to NOT release the mail object, that's why my Outlook item wrapper even had this option in the factory method. In the ADX blogs, you, Andrei and others always emphasize the need to release COM objects as early as possible. But I did not realize that I can not release any of the mail objects I use between opening a mail in inspector, and creating a new inspector window for the anwer.

Many thanks again for your excellent support.
Posted 14 Aug, 2017 04:01:51 Top