Click here to Skip to main content
12,403,811 members (68,501 online)
Rate this:
Please Sign up or sign in to vote.
Hello Everybody.

I have strange situation in my applicaion (MS Outlook Addin).

I work with MailItem object in Item.Open event handler.
I get Inspector object calling MailItem.GetInspector property.
When I try to set handler for Inspector.Close event InvalidComObjectException with message "COM object that has been separated from its underlying RCW cannot be used" is thrown.

Code snippet:
MailItem mailItem;
InspectorEvents_10_Event inspectorEvents = mailItem.GetInspector;
inspectorEvents.Close += delegate {.....}; //Exception occurs

In my opinion, it is impossible situation. There is no any call of Marshal.ReleaseComObject between getting Inspector and using it.

This error is reproduced very rarely, so I can't catch it while debugging.

Any suggestions?
Posted 1-Feb-11 7:44am
Updated 25-May-16 5:45am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

One likely cause is that you are accessing a single threaded COM object from different threads. In your case, the event handler may not be running on the thread that created the COM object.
ielsky 2-Feb-11 12:52pm
Thank you for you answer.

I checked application log. Thread id in the place where error occurs is the same as thread id in NewInspector handler (where we get Inspector). So, I need another idea.
ncc01 12-Feb-11 3:09am
Hi, I am facing the same problem it is really difficult to debug it because the test case is not unique. did you find a solution? I really appreciate your help in advance.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

You need have strong reference to event.
Anything like this
MailItem mailItem;
CloseEvent closeEvent;
InspectorEvents_10_Event inspectorEvents = mailItem.GetInspector;
closeEvent = inspectorEvents.Close;
closeEvent += delegate {.....};

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 25 May 2016
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100