WPF应用程序抛出Win32Exception:尝试访问无效地址

时间:2015-06-17 18:18:11

标签: c# wpf winapi

我有一个在Windows 8上运行的WPF kiosk应用程序,有时候经常在几个小时之后,应用程序会在CurrentDomain上抛出异常。

该应用程序使用一些计时器来更新屏幕上的时钟和网络连接指示器。相应的事件处理程序已经有try..catch块。

我有一个附加到AppDomain.CurrentDomain.UnhandledException的异常处理程序,它通过邮件向我发送异常的堆栈跟踪,如下所示。

System.ComponentModel.Win32Exception: Tentativa de acessar endereço inválido

Source: WindowsBase
NativeErrorCode: 487
ErrorCode: -2147467259

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

我在代码中添加了异常处理程序,但我仍然无法捕获异常。我查看了.NET Framework源代码,发现了GetNameCore()的实现,但我仍然不知道可能导致问题的原因。

[SecurityCritical,SecurityTreatAsSafe]
override protected string GetNameCore()
{ 
    string name = base.GetNameCore();

    if(name == string.Empty) 
    {
        Window window = (Window)Owner; 

        if(!window.IsSourceWindowNull)
        {

            StringBuilder sb = new StringBuilder(512);
            UnsafeNativeMethods.GetWindowText(new HandleRef(null, window.CriticalHandle), sb, sb.Capacity); 
            name = sb.ToString(); 

            if (name == null) 
                name = string.Empty;
        }
    }

    return name;
} 

有没有人知道这个例外可能来自何处以及如何避免它?

0 个答案:

没有答案