2

I am running into a problem where my application is crashing a WPF application due to EventHooking. Below is simple code that I tried and was still able to recreate the crash. This is only when running against some sort of custom tree view that has a number of its nodes expanded.

{
...
_focusChangedHandler =  OnFocusChanged;
_focusChangedGCHandle = GCHandle.Alloc(_focusChangedHandler);
_focusChangedHookId = User32.SetWinEventHook(User32.EVENT_OBJECT_FOCUS,
    User32.EVENT_OBJECT_FOCUS, IntPtr.Zero, _focusChangedHandler,
    0,0,User32.WINEVENT_OUTOFCONTEXT);
...
}

private void OnFocusChanged(IntPtr hWinEventHook, uint eventType,
                            IntPtr hwnd,
                            int idObject, int idChild, uint dwEventThread,
                            uint dwmsEventTime)
{
}

I also had the customer test this with an explicit new User32.WinEventDelegate(OnFocusChanged) and without the GCHandle.Alloc. As well as a combination of all those scenarios. This did NOT solve the problem

Does anybody have any idea why this might be happening?

*We did have a problem with this WPF application and creating memory/performance issues around virtualization being cancelled by IAccessible calls, however we have since removed those calls.

EDIT PER REQUEST

The only information I have on the third party is from an event log:

Faulting application name: THIRDPARTY.exe, version: x.y.z, time stamp: 0x4fb0d031
Faulting module name: clr.dll, version: 4.0.30319.296, time stamp: 0x50484aa9
Exception code: 0xc00000fd
Fault offset: 0x00001ac6
Faulting process id: 0x6f50
Faulting application start time: 0x01cdf9a521428158
Faulting application path: C:\Program Files (x86)\THIRDPARTYEXEPATH
Faulting module path: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Report Id: d8e42ff6-6598-11e2-9614-441ea14b96e0

The Exception code is a stack overflow as far as my research tells me

Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
  • you actually do nothing in `OnFocusChanged`? –  Feb 04 '13 at 20:47
  • @Will In the production app, we do something, but I have limited it down and found the crash still occurs when doing nothing :( – Justin Pihony Feb 04 '13 at 21:15
  • 1
    Can you post the repeating part of the stack trace? – 500 - Internal Server Error Feb 04 '13 at 21:51
  • @500-InternalServerError Updated with the best info I have – Justin Pihony Feb 04 '13 at 22:09
  • The zeroes in the registering function mean that you are hooking all the threads in your desktop... maybe you are receiving so many events at the same time that your stack simply explodes! – rodrigo Feb 04 '13 at 22:35
  • @rodrigo, out-of-proc winevents get queued to the thread's message queue, so usually won't affect the stack, so not the problem here with the empty callback. However, if the callback does something that can process messages- eg. calls SendMessage, reentrancy can happen, and that can lead to large stacks, and messages being apparently processed out-of-order! – BrendanMcK Feb 05 '13 at 03:13
  • @JustinPihony - so your app is plain WinForms, and it's the separate target WPF app that's crashing, right? Even when all you are doing is listening for events, with no actual processing of those events? That would sound like some sort of bug in the WPF implementation. It likely has code that is "if(someone is listening for events) { do some house-keeping; fire the event }", and the housekeeping code is going awry. If that's the case, there may be no easy fix on your side... Do you have any other details about the WPF app? – BrendanMcK Feb 05 '13 at 03:19
  • 2
    @JustinPihony - might be worth seeing if this repros with the [Accessible Event Watcher](http://msdn.microsoft.com/en-us/library/windows/desktop/dd317979(v=vs.85).aspx) tool. Set to WinEvents (Out of context) in Mode menu, then in Mode/Settings... select just OBJ_FOCUS, and uncheck all object properties and checkboxes, and perhaps check only Event Information option of HWND. This will essentially cause the tool to do something similar to your code above, listening to only _FOCUS events, but doing nothing in response; may at least show issue is not in your app. – BrendanMcK Feb 05 '13 at 04:19
  • @BrendanMcK Thanks, I kind of figured it might be something out of our control. I did create an app that does ONLY this, however I would like to try something not of my creation – Justin Pihony Feb 05 '13 at 04:41

0 Answers0