简体   繁体   中英

random crash in WPF application : "Invalid index at MS.Win32.UnsafeNativeMethods.GetWindowText(...)"

We experience following crash at random moments in our WPF application:

 Invalid index at MS.Win32.UnsafeNativeMethods.GetWindowText(HandleRef hWnd, StringBuilder lpString, Int32 nMaxCount)
   at System.Windows.Automation.Peers.WindowAutomationPeer.GetNameCore()
   at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
   at System.Windows.ContextLayoutManager.fireAutomationEvents()
   at System.Windows.ContextLayoutManager.UpdateLayout()
   at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
   at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
   at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run(Window window)
   at System.Windows.Application.Run()
 

Most of the time the crash occurs on any call to "ShowDialog()" in the WPF code, sometimes also on other calls (unclear which exactly, the stack trace just mentions the top-level "Application.Run()")

The problem starts occurring randomly after having accessed some javascript code in a CefSharp embedded browser component, which adds WebRTC streaming functionality to the WPF application. The crashes stopped occurring as soon as we replaced the CefSharp component with Microsofts WebView2 component.

On a few occasions in the past we encountered the exact same random crash when our WPF application was running alongside 2 other third party applications:

  • a "TabletInputService", part of Smart Technologies whiteboard software
  • a screen capture driver ("VHScrCap" of Split Media Labs)

At that moment we found that the crash even occurred in a dummy WPF test application which contained nothing but 1 "ShowDialog()" call. This lead us to the conclusion that we could change nothing in our WPF application code to avoid the crashes. Only "solution" was to replace above screen capture driver with the DirectShow "screen-capture-recorder"

My question is: what could be the root cause of this window handle corruption, and what could we do to avoid the resulting crashes in a WPF application?

I hope I found the cause of this issue. I again encountered the same crash, but again under different conditions: after calling VideoCapturer.EnumerateDevices() on the OpenTok.NET Nuget library, the crash started occurring when expanding any combobox in any WPF modal dialog.

Now it looks like the instability is caused by not specifying the Title property of a WPF window. In our application most of the dialogs are headerless (WindowStyle="None"), as we design our own window headers, so setting the Title property is unnecessary/irrelevant. This must confuse the Win32 API call "GetWindowText()", resulting in a nasty crash. Seems like a serious WindowOS/WPF bug to me.

After scanning our full application source code and specifying a (dummy) Title for each and every dialog, the crash stopped occurring (at least for now).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM