【问题标题】:'Access violation' error in WinUI 3 desktop app cannot be 'caught'?无法“捕获”WinUI 3 桌面应用程序中的“访问冲突”错误?
【发布时间】:2022-12-16 20:03:40
【问题描述】:

我有一个相当复杂的 WinUI 3 桌面应用程序(调试模式下的 v1.2、.NET 7、x64)一直失败(仅在特定的事件序列之后),并在 Output 窗口中显示来自 Visual Studio 2022 Enterprise 的消息说:

程序“[28716] xxxxxxx.exe”已退出,代码为 3221225477 (0xc0000005) '访问冲突'。

该应用程序在 UI 线程上启动一个辅助窗口

    if (App.ShellPage.SettingsStatusWindow)
    {
        StatusWindow = new StatusWindow();                               // create new window
        StatusWindow.Activate();
    }
    else
    {
        WeakReferenceMessenger.Default.Send(new CloseWindowMessage());   // close windows
    }

并且 StatusWindowCloseWindowMessage 处理程序中关闭,就像这样

    WeakReferenceMessenger.Default.Register<CloseWindowMessage>(this, (r, m) =>
    {
        WeakReferenceMessenger.Default.Unregister<TraceMessage>(this);
        WeakReferenceMessenger.Default.Unregister<CloseWindowMessage>(this);
        Close();
    });

如果收到CloseWindowMessage,或者在StatusWindow_Closed 处理程序中单击StatusWindow 标题栏上的Close 按钮。

    private void StatusWindow_Closed(object sender, WindowEventArgs args)
    {
        Closed -= StatusWindow_Closed;
        WeakReferenceMessenger.Default.UnregisterAll(this);
        WeakReferenceMessenger.Default.Send(new WindowClosedMessage());
    }

WeakReferenceMessenger 类来自CommunityToolkit.Mvvm.Messenger

尽管消息在Output 窗口中,但它不是来自任何TraceDebug 代码,并且似乎没有引发任何可捕获的Exception,因为App 启动包括:

UnhandledException += App_UnhandledException;
System.AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Microsoft.UI.Xaml.Application.Current.UnhandledException += Current_UnhandledException;

但没有处理程序被调用。

我最好的猜测是,不知何故,一条消息在取消注册消息后被发送到ClosingStatusWindow,或者ClosedStatusWindow。奇怪的是,通过发送 CloseWindowMessage 关闭 StatusWindow 将在大约一秒钟内导致违规,而通过单击 Close 按钮关闭一次 StatusWindow 不仅不会导致错误,而且任何新的 StatusWindows之后打开永远不会导致错误,无论它们是如何关闭的。这让我想知道这是否是一个初始化问题。

我试图通过剥离所有其他逻辑并构建一个仅包含窗口的小应用程序来隔离问题,但我似乎无法生成 Access violation 错误。

关于如何让 .NET 抛出可捕获的错误以帮助解决问题的任何想法?或者,关于如何隔离有问题的消息或对象引用的建议?提前谢谢了。

【问题讨论】:

  • 你可以让它在调试模式下失败吗?好吧,然后按如下方式设置调试设置:1.启用本机代码调试 检查,2.只是我的代码: 未勾选.3. 启用 .NET Framework 源步进: 检查 4.* 访问冲突异常(在“Win32 异常”类别的异常设置下)检查.像这样调试,它应该会把你带到问题发生的地方
  • 非常感谢。我忘记了混合模式调试。调试器现在告诉我'在 xxxxxx.exe 中的 0x00007FFD09273127 (Microsoft.ui.xaml.dll) 抛出异常:0xC0000005:访问冲突读取位置 0x0000000000000000。所以它是对不存在的对象的引用。堆栈也提供了更多信息。我只需要筛选一下。

标签: c# messaging visual-studio-debugging access-violation unhandled-exception


【解决方案1】:

要调试 Xaml 代码,请在后面的代码中用 Try-Catch 包围您的 Initialize 方法,正如我之前在此处展示的那样: XAML-debug

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-14
    • 2021-05-13
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    相关资源
    最近更新 更多