문제

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?

도움이 되었습니까?

해결책

I know this sounds silly but in order to listen to the DebuggerEvents events you need to maintain a reference to DebuggerEvents 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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top