microsoft-ui-xaml icon indicating copy to clipboard operation
microsoft-ui-xaml copied to clipboard

WebView2 control throws OutOfMemoryException on call to EnsureCoreWebView2Async() after being loaded too many times

Open tflopez opened this issue 1 year ago • 1 comments

Describe the bug

Our WinUI 3 app allows users to navigate to various pages to define a model, compute, and review outputs. Some of these pages have WebView2 controls on them to show their content. Once we've loaded a WebView2 control 252 times, the 253rd attempt throws an OutOfMemory exception when we call EnsureCoreWebView2Async() when initializing the WebView2 control.

This was tested with the following configurations with the same behavior:

  • Windows 10 (10.0.19045), WebView Runtime 125.0.2535.67 and 124.0.2478.109
  • Windows 11 (10.0.22631), WebView Runtime 125.0.2535.67

Steps to reproduce the bug

WebViewTest.zip

  1. Run the attached application's Debug|x86 unpackaged configuration
  2. Click on a NavigationViewItem to load a page containing a single WebView2 control
  3. Continue clicking on a NavigationViewItem to load subsequent pages
  4. Once you start to load the 253rd page, it will crash with an OutOfMemoryException when the page calls the WebView2's EnsureCoreWebView2Async() method.

Expected behavior

We should be able to load and unload a WebView2 control as many times as necessary, as is possible with other controls like TextBoxes, RadioButtons, etc.

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.5.3: 1.5.240428000

Windows version

Windows 11 (22H2): Build 22621, Windows 10 (21H2): Build 19044

Additional context

If I don't call WebView.Close() from the Page's Unloaded event, I have been able to load the WebView2 control as many as 268 times before crashing.

I've made a similar bug report in the Microsoft Edge WebView2Feeback repository. Please let me know if that or somewhere else is a more appropriate place to report this issue. Thank you!

tflopez avatar Jun 03 '24 14:06 tflopez

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

github-actions[bot] avatar Jun 03 '24 14:06 github-actions[bot]

I've had a look at this, and it looks to me like it may be an issue with C# garbage collection. In your test app (thanks so much for that, by the way!), I added this after WebView2.Close();:

                WebView.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Normal, () =>
                {
                    GC.Collect();
                });

I no longer see the memory usage grow unboundedly larger with that added. Could you confirm? It's possible I'm missing something, so feel free to reopen if so.

llongley avatar Aug 20 '24 05:08 llongley

Thanks for looking at this! After you posted, I ran through some tests before trying your solution and found that I could no longer reproduce the initial issue. I loaded the WebView2-containing page more than 600 times without a crash before giving up. So, I think the problem has already been addressed in some way.

tflopez avatar Sep 04 '24 17:09 tflopez