Reputation: 10566
I wrote a visual studio extension package that subscribes to DebuggerEvents.OnEnterBreakMode event. But this event is never get raised.
Here is some code:
protected override void Initialize()
{
base.Initialize();
applicationObject = (DTE2) GetService(typeof (DTE));
applicationObject.Events.BuildEvents.OnBuildDone += BuildEventsOnOnBuildDone;
applicationObject.Events.DebuggerEvents.OnEnterBreakMode += DebuggerEventsOnOnEnterBreakMode;
}
But method DebuggerEventsOnOnEnterBreakMode
is never called. Method BuildEventsOnOnBuildDone
is called.
Why this can happen?
Upvotes: 1
Views: 682
Reputation: 754745
I know this sounds silly but in order to listen to the DebuggerEvents
events you need to maintain a reference to DebuggerEvent
s itself.
DebuggerEvents _debuggerEvents;
protected override void Initialize() {
applicationObject = (DTE2) GetService(typeof (DTE));
_debuggerEvents = applicationObject.Events.DebuggerEvents;
_debuggerEvents.OnEnterBreakMode += DebuggerEventsOnOnEnterBreakMode
The reason for this is subtle. DebuggerEvents
is actually a COM / CCW object which is created on demand when the DTE.DebuggerEvents
property is accessed. The event handler code doesn't keep the CCW alive hence the next GC potentially collects the DebuggerEvents
property and takes the event handler with it.
It's a really strange bug that is specific to CCW and events. I've heard it referred to as "the most vexing bug ever" and it's not far from the truth
Upvotes: 11