Adding event handlers COM seperated from RCW

Discussion created by agray1 on May 28, 2013
Latest reply on Aug 7, 2013 by jeffmatson
Hello Forum,

I have this intermittent problem, (happens about 60% of the time.) 
I have an extension (not addin) where I listen for a start editing event
In the start editing I start other events listeners, specificaly activeviewevents item added, item removed and mapevents featureclass changed.
The problem is often (60% of the time but never in debug mode) I get an exception when adding the event handler
System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.

this is a snippet in the startediting event handler
          m_actViewEvents = CType(m_map, IActiveViewEvents_Event)
          Trace.WriteLine("active view events retrieved")
          AddHandler m_actViewEvents.ItemAdded, AddressOf ItemAdded 'exception is thrown here
          Trace.WriteLine("item added event handler added")

I set the m_map in the start editing.  I tried casting the to the Iactiveviewevents (better design), I tried setting the m_map from the editor, I tried using the document focus map.  I tried declaring the m_Editor and m_map member vars as shared (static)  I tried declaring the m_activeViewEvents as shared. 
As far as I know everything is done on the main thread.
I would rather not populate the m_map variable before the edit session is started since the focus map could change.