Crash on calling GetFocusedElement from a thread

73 Views Asked by At

I have an Electron app with a native node module written in C++, built with MSVC compiler and linked using CMakeJS.

The node module has hook callbacks installed (SetWindowsHookEx) in separate threads for a mouse click (WH_MOUSE_LL) or a keydown (WH_KEYBOARD_LL). It calls GetFocusedElement whenever the user clicks or presses a key. The call is retried three times with a Sleep(10) in case the return code is UIA_E_ELEMENTNOTAVAILABLE.

Is there any particular pre-condition for GetFocusedElement that I need to enforce to avoid this fatal crash? I have checked the docs and I didn't find any such precondition. I would prefer to avoid any fatal crashes even if that means not finding any focused element.

Error trace:

OS Version: Windows 10.0.19044 (3086)
Report Version: 104

Crashed Thread: 10164

Application Specific Information:
Fatal Error: EXCEPTION_ACCESS_VIOLATION_READ / 0xffffffffffffffff

Thread 10164 Crashed:
0   uiautomationcore.dll            0x7fffb46cdfe2      LocalUiaNodeProxy::RealRemoteNodeFromProviderEntryPoint
1   uiautomationcore.dll            0x7fffb46fb1fb      LocalUiaNodeProxy::RemoteNodeFromProviderEntryPoint
2   uiautomationcore.dll            0x7fffb46fb15e      LocalUiaNodeProxy::RemoteNodeFromProviderEntryPoint
3   uiautomationcore.dll            0x7fffb47267c5      UiaNodeFactory::InProcProxyFromHwnd
4   uiautomationcore.dll            0x7fffb471a47a      ClientProxyManager::InternalGetProxyProvider
5   uiautomationcore.dll            0x7fffb471a087      ClientProxyManager::GetProxyProvider
6   uiautomationcore.dll            0x7fffb4709767      UiaNodeFactory::CompleteNodeInfo
7   uiautomationcore.dll            0x7fffb4708289      UiaNodeFactory::FromPartialNodeInfo
8   uiautomationcore.dll            0x7fffb472643c      UiaNodeFactory::FromLocalProvider
9   uiautomationcore.dll            0x7fffb472654f      UiaNodeFactory::FromLocalProvider
10  uiautomationcore.dll            0x7fffb46e8b4f      UiaNode::ProviderDrillForPointOrFocus
11  uiautomationcore.dll            0x7fffb46e84e4      UiaNode::ProviderDrillForPointOrFocusInternal
12  uiautomationcore.dll            0x7fffb46e83f1      InProcClientAPIStub::UiaNode_DrillForPointOrFocus
13  uiautomationcore.dll            0x7fffb46c60fd      ComInvoker::CallTarget
14  uiautomationcore.dll            0x7fffb46fa998      InProcClientAPIStub::InvokeInProcAPI
15  uiautomationcore.dll            0x7fffb46be327      UiaNode::CrossProcess_DrillForPointOrFocus
16  uiautomationcore.dll            0x7fffb4778d7b      CUIAutomation::GetFocusedElement
17  nativeapp.node                  0x7fffdec1187d      GetFocusedElementRetry (iohook.cc:182)
18  nativeapp.node                  0x7fffdec1432f      focusChangeHandler (iohook.cc:304)
19  nativeapp.node                  0x7fffdec0fbfa      <lambda>::operator() (iohook.cc:779)
20  nativeapp.node                  0x7fffdec0c70d      [inlined] std::invoke (type_traits:1564)
21  nativeapp.node                  0x7fffdec0c70d      std::thread::_Invoke<T> (thread:55)
22  ucrtbase.dll                    0x7fffe3b91bb1      thread_start<T>
23  KERNEL32.DLL                    0x7fffe48d7613      BaseThreadInitThunk
24  ntdll.dll                       0x7fffe63c26f0      RtlUserThreadStart
1

There are 1 best solutions below

0
KingQQ On

The error EXCEPTION_ACCESS_VIOLATION_READ always happened when the structed object didn't get the correct memory address and can't be read as a completed structed object In this case, would you please check the parameter IUIAutomationElement **element and make sure it was generated correctly.