Skip to content
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

Win platform WebView cannot be release after its parent window get close #22972

Closed
jingliancui opened this issue Jun 11, 2024 · 8 comments · Fixed by #23540
Closed

Win platform WebView cannot be release after its parent window get close #22972

jingliancui opened this issue Jun 11, 2024 · 8 comments · Fixed by #23540
Labels
area-controls-webview WebView fixed-in-8.0.80 fixed-in-9.0.0-preview.7.24407.4 platform/windows 🪟 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Milestone

Comments

@jingliancui
Copy link
Contributor

Description

Please see the Reproduce

Steps to Reproduce

  1. Open the sln using VS 2022
  2. Press F5 to start debuging
  3. Enter 50 or more to the Entry control
  4. Click "new windows" Button
  5. Close all WebView windows except the MainPage window

Expect result:
Memory should be reduced

Actual result:
Memory does not get reduced

Link to public reproduction project repository

https://github.com/jingliancui/MAUIWebViewMultipleWinIssue

Version with bug

8.0.40 SR5

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

windows10.0.19041.0

Did you find any workaround?

no

Relevant log output

no
@jingliancui jingliancui added the t/bug Something isn't working label Jun 11, 2024
Copy link
Contributor

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 and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

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

@ninachen03 ninachen03 added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Jun 12, 2024
@ninachen03
Copy link

Verified this issue with Visual Studio 17.11.0 Preview 1.1 (8.0.40 & 8.0.21). Can repro on windows platform with sample project.

@jsuarezruiz jsuarezruiz added this to the Backlog milestone Jun 12, 2024
@jingliancui
Copy link
Contributor Author

@mattleibow @jsuarezruiz @PureWeen
May I know if any workarounds can be used before this gets fix?
For example manually dispose the control or handler etc...

@samhouts samhouts removed s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Jul 3, 2024
@jonathanpeppers
Copy link
Member

jonathanpeppers commented Jul 9, 2024

@jingliancui for now, can you do something like this in your Page:

    protected override void OnDisappearing()
    {
        base.OnDisappearing();

        webView.Handler?.DisconnectHandler();
    }

(I put x:Name="webView" in the XAML)

This seems to solve the issue in this sample:

image

jonathanpeppers added a commit to jonathanpeppers/maui that referenced this issue Jul 10, 2024
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();
    }
@jingliancui
Copy link
Contributor Author

jingliancui commented Jul 10, 2024

Hi @jonathanpeppers
PlatformView cannot be null here

not sure which platformview cause this exception

image

image

@jingliancui
Copy link
Contributor Author

@jonathanpeppers You can try my sample repo, it also can reproduce the exception, even I updated the nuget to latest version.

@jonathanpeppers
Copy link
Member

This has the fix for the underlying problem, if you are interested:

It would no longer require DisconnectHandler() to be called.

@jingliancui
Copy link
Contributor Author

This has the fix for the underlying problem, if you are interested:

It would no longer require DisconnectHandler() to be called.

Cool! May I know if the fix will be released at next sr8? Thanks

@github-actions github-actions bot locked and limited conversation to collaborators Sep 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-webview WebView fixed-in-8.0.80 fixed-in-9.0.0-preview.7.24407.4 platform/windows 🪟 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants