0

I have a fairly complex WinUI 3 desktop app (v1.2, .NET 7, x64 in Debug mode) that keeps failing (after a specific sequence of events only) with a message from Visual Studio 2022 Enterprise in the Output window that says:

The program '[28716] xxxxxxx.exe' has exited with code 3221225477 (0xc0000005) 'Access violation'.

The app starts a secondary window on the UI thread with

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

and StatusWindow is closed in the CloseWindowMessage handler like so

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

if a CloseWindowMessage is received or in the StatusWindow_Closed handler if the Close button on the the StatusWindow title bar is clicked.

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

The WeakReferenceMessenger class is from CommunityToolkit.Mvvm.Messenger.

Although the message is in the Output window, it is not from any Trace or Debug code and does not seem to raise any catchable Exception as App startup includes:

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

but no handler gets called.

My best guess is that somehow, a message is being sent to either a Closing StatusWindow after it has unregistered for messages, or a Closed StatusWindow. Curiously, closing a StatusWindow by sending a CloseWindowMessage will cause the violation within about a second whereas closing the StatusWindow once by clicking the Close button not only doesn't cause the error, but any new StatusWindows opened afterward never cause the error regardless of how they're closed. This makes me wonder if it's an initialization issue.

I've tried to isolate the problem by stripping out all the other logic and building a small app with just the window, but I can't seem to generate the Access violation error.

Any ideas on how I can get .NET to throw a catchable error to help troubleshoot? Alternatively, an suggestions on how I might isolate the offending message or object reference? Many thanks in advance.

aturnbul
  • 347
  • 2
  • 12
  • 1
    You can make it fail in Debug mode? Well then set your debug settings as follows: **1. Enable native code debugging** *checked*, **2. Just my code:** *un-checked*. **3. Enable .NET Framework source stepping:** *checked* **4.* Access Violation** exception (under Exception Settings in the "Win32 Exceptions" category) *checked*. Debug like that and it should take you right to where the problem is occurring – Joe Nov 11 '22 at 19:17
  • 1
    Thanks VERY much. I forgot the mixed-mode debugging. The debugger now tells me _'Exception thrown at 0x00007FFD09273127 (Microsoft.ui.xaml.dll) in xxxxxx.exe: 0xC0000005: Access violation reading location 0x0000000000000000.'_ so it's a reference to a non-existent object. The stack is a lot more informative, too. I just have to sift through it. – aturnbul Nov 11 '22 at 20:09
  • What settings are needed for VS 2022? – John Glen Aug 14 '23 at 19:07

1 Answers1

0

To debugging the Xaml code please surround your Initialize method in the code behind with a Try-Catch as I previously showed here: XAML-debug