-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[windows] fix memory leak in WebView #23540
Conversation
Fixes: dotnet#20283 Fixes: dotnet#22972 Context: https://github.com/davide-cavallini/webview-winUI-maui-leak The above sample on Windows was leaking because it does the following: 1. Your app has a single `Window` 2. Navigate to a page with a `WebView` 3. Navigate away Because the `Window` remains alive, `WebViewHandler` subscribes to `Window.Closed` which keeps a reference to the `WebView` and keeps it alive indefinitely. I was able to reproduce this in a test, that keeps the `Window` alive before calling `AssertionExtensions.WaitForGC()`. The solution was to move the `Window.Closed` subscription to the `WebView2Proxy` nested class. This makes sure that the `WebView`, its handler, etc. can be collected *before* the `Window` is closed. I also found a secondary issue while debugging, if you call: webView.Close(); // MauiWebView or WebView2 If `CoreWebView2` is not initialized, it will throw a C++ exception. We can instead do: if (webView.CoreWebView2 is not null) { webView.Close(); }
There is a Windows test failure, but not sure if it's related to my changes. I reran it again. |
Failing tests not related. |
Hi @jonathanpeppers, I tried the repro project in the following way
For me the bug does not seem solved, in fact I don't see in Task Manager the memory free up, the situation seems the same as reported in the bug. If the following row is added in OnDisappearing(), the memory is freed also in Task Manager Am I doing something wrong? 8.0.80-ci.net8.24374.2 seems the latest nightly version available for the moment |
@bronteq the fix here prevents every If there are If calling |
Sorry @jonathanpeppers I misunderstood your PR. My initial issue was #21194 If the issues are different, probably my issue should be reopened, because it is not solved yet and the amount of ram that is not freed up can be very high (think of an industrial pc with limited specs, the program becomes unusable). Calling |
I am still seeing the issue on 8.0.80. Am I missing something? |
Fixes: #20283
Fixes: #22972
Context: https://github.com/davide-cavallini/webview-winUI-maui-leak
The above sample on Windows was leaking because it does the following:
Your app has a single
Window
Navigate to a page with a
WebView
Navigate away
Because the
Window
remains alive,WebViewHandler
subscribes toWindow.Closed
which keeps a reference to theWebView
and keeps it alive indefinitely.I was able to reproduce this in a test, that keeps the
Window
alive before callingAssertionExtensions.WaitForGC()
.The solution was to move the
Window.Closed
subscription to theWebView2Proxy
nested class. This makes sure that theWebView
, its handler, etc. can be collected before theWindow
is closed.I also found a secondary issue while debugging, if you call:
If
CoreWebView2
is not initialized, it will throw a C++ exception.We can instead do: