c++visual-studiowinui-3c++-winrtwindows-app-sdk

Natvis C++/WinRT: Exception in cppwinrt_visualizer::EvaluateVisualizedExpression


The code below results in this exception even with an empty XML(only the page tag):

Natvis C++/WinRT: Exception in cppwinrt_visualizer::EvaluateVisualizedExpression
Natvis C++/WinRT: Exception in cppwinrt_visualizer::EvaluateVisualizedExpression
Natvis C++/WinRT: Exception in cppwinrt_visualizer::EvaluateVisualizedExpression
Natvis C++/WinRT: Exception in cppwinrt_visualizer::EvaluateVisualizedExpression

The error happens on this line rootFrame.Navigate(xaml_typename<ProjectNamespace::FirstPage>(), box_value(e.Arguments()));

void App::OnLaunched(LaunchActivatedEventArgs const& e)
{
    window = make<MainWindow>();

    Frame rootFrame = Frame();
    rootFrame.NavigationFailed({this, &App::OnNavigationFailed});
    rootFrame.Navigate(xaml_typename<ProjectNamespace::FirstPage>(),
                       box_value(e.Arguments()));

    window.Content(rootFrame);
    window.Activate();
}

Any idea what could be the problem?


Solution

  • The diagnostic message doesn't indicate a problem with the code. It originates in C++/WinRT's debug visualizer implementation attempting to supply human-readable insights to the debugger. This is the faulting function:

    HRESULT cppwinrt_visualizer::EvaluateVisualizedExpression(...)
    {
        try
        {
            // ...
        }
        catch (...)
        {
            // If something goes wrong, just fail to display object/property.  Don't take down VS.
            NatvisDiagnostic(pVisualizedExpression, 
                L"Exception in cppwinrt_visualizer::EvaluateVisualizedExpression", NatvisDiagnosticLevel::Error, to_hresult());
            return E_FAIL;
        }
    }
    

    The comment suggests this is a known issue but hasn't been fully fixed. Damage is contained, and instead of crashing the debugger, this function doesn't visualize an object or property if it finds that it can't, for any number of reasons.

    Repro

    This is enough information to construct a repro:

    1. Prepare a Visual Studio instance for WinUI 3 development with C++/WinRT.
    2. Create a new "Blank App, Packaged (WinUI 3 in Desktop)" project.
    3. Open Tools → Options..., navigate to Debugging → Output Window, and set the "Natvis diagnostic messages (C++ only)" verbosity to "Error".
    4. Set a breakpoint in App::OnLaunched() on this line:
      🔴 window = make<MainWindow>();
      
    5. Select Debug → Start Debugging.
    6. When the breakpoint is hit open Debug → Windows → Autos.
    7. Step over this line (Debug → Step Over).

    At this point, the "Debug" output pane will show several errors like the following:

    Natvis C++/WinRT: Exception in cppwinrt_visualizer::EvaluateVisualizedExpression
    

    Solution

    Since the issue is inside the C++/WinRT debug visualizer implementation, there's nothing we can do to fix it. It would need to be addressed by one of the Windows App SDK, MSBuild, or C++/WinRT teams. Given past experience with problems where XAML and C++/WinRT meet, I'm reluctant to even file an issue.

    Users are left with two options: