Skip to content

Bug: InvalidOperationException when closing a child window (UWP .NET 9) #1955

@kinex

Description

@kinex

Description

When I open a new child window in UWP (.NET 9) and try to close it using Window.Current.Close(), the following exception is thrown:

System.InvalidOperationException
  HResult=0x80131509
  Message=Cannot release objects that are not runtime wrappers of native WinRT objects.
  Source=WinRT.Runtime
  StackTrace:
   at WinRT.DefaultComWrappers.ReleaseObjects(IEnumerable objects) in WinRT\DefaultComWrappers.cs:line 145

This works without issues in UWP .Net Native.

However, closing the child window with ApplicationView.GetForCurrentView().TryConsolidateAsync() seems to work better. That said, it still crashes in my production app if I place a breakpoint on that line, but works fine without the breakpoint. Interestingly, I could not reproduce this issue in the provided sample app.

I also observed that the crash occurs when building for Debug/x64, but it works correctly when building for Debug/x86.

Steps To Reproduce

  1. Open a new child window
        async void OpenChildWindowButton_Click(object sender, RoutedEventArgs e)
        {
            CoreApplicationView newView = CoreApplication.CreateNewView();
            int newViewId = 0;
            await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                Frame frame = new Frame();
                frame.Navigate(typeof(ChildPage), null);
                Window.Current.Content = frame;
                Window.Current.Activate();
                newViewId = ApplicationView.GetForCurrentView().Id;
            });

            bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
        }
  1. Close the child window
        async void CloseChildWindowButton_Click(object sender, RoutedEventArgs e)
        {
            // crash
            Window.Current.Close();
            
            // no crash
            //await ApplicationView.GetForCurrentView().TryConsolidateAsync();
        }

When testing make sure the build target Debug/x64 is selected.

Expected Behavior

No crash.

Version Info

VS 17.13.4

Additional Context

Sample app to reproduce the issue.

App1.zip

Activity

added theissue type on Mar 26, 2025
Sergio0694

Sergio0694 commented on Mar 29, 2025

@Sergio0694
Member

I can repro locally, will investigate. Thank you so much for including such a minimal repro and all the info! 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @Sergio0694@kinex

      Issue actions

        Bug: InvalidOperationException when closing a child window (UWP .NET 9) · Issue #1955 · microsoft/CsWinRT