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

[BUG] .Net MAUI SKCanvasView memory leak on IOS #2923

Closed
1 task done
nevse opened this issue Jul 4, 2024 · 5 comments · Fixed by #2955
Closed
1 task done

[BUG] .Net MAUI SKCanvasView memory leak on IOS #2923

nevse opened this issue Jul 4, 2024 · 5 comments · Fixed by #2955
Labels

Comments

@nevse
Copy link

nevse commented Jul 4, 2024

Description

If you place SKCanvasView on Page it cause a memory leak.

Code

I create a simple app wich reproduce this leak https://github.com/nevse/maui-tests-SKCanvasViewLeak

Step to reproduce:

  1. Click button "Open Page"
  2. Navigate back from page with SKCanvasView
  3. Click button "Check Memory Leak"

I use such code to detect memory leak:

WeakReference pageRef;
void OnOpenPage(object sender, EventArgs e)
{
    var page = new SKCanvasViewPage();
    Navigation.PushAsync(page);
    pageRef = new WeakReference(page);
}
void OnCheckMemoryLeak(object sender, EventArgs e)
{
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    if (pageRef != null && pageRef.IsAlive)
    {
        DisplayAlert("Memory Leak", "Memory Leak Detected", "OK");
    }
    else
    {
        DisplayAlert("Memory Leak", "No Memory Leak Detected", "OK");
    }
}

Expected Behavior

No response

Actual Behavior

No response

Version of SkiaSharp

2.88.3 (Current)

Last Known Good Version of SkiaSharp

Other (Please indicate in the description)

IDE / Editor

Visual Studio Code (macOS)

Platform / Operating System

iOS

Platform / Operating System Version

No response

Devices

No response

Relevant Screenshots

No response

Relevant Log Output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@nevse nevse added the type/bug label Jul 4, 2024
@Alexgoon
Copy link

Some DevExpress components have a memory leak due to this issue, which prevents the entire page from being released. This problem is affecting many of our customers. @mattleibow, could you please take a look at this when you have a chance?

@janne-hmp
Copy link

@mattleibow @davidortinau I think this should be critically prioritized, since you can't use SkiaSharp with .NET MAUI on iOS seriously. I think my memory corruption problem mentioned in #2840 maybe because of the same or related issue.

@janne-hmp
Copy link

I just made a test app (see #2951) that confirms this by crashing my iPad by trying to open and close the same page 500 times. It crashes after opening the page c. 145th time.

@mattleibow
Copy link
Contributor

I think I know what is happening. After all our work to fix leaks in Maui itself, it appears that an event on the platform view to the handler keeps everything.

The platform view event keeps the handler alive, the handler keeps the Xaml view alive and the xaml view keeps the parent alive. The virtual view also keeps the handler alive and the handler keeps the platform view. So basically a big web of survival.

However, there has also been a fix in Maui to break the virtual view and it's parent: dotnet/maui#22561

We just have to do something like this: dotnet/maui#18682

Thanks to the hard work of @jonathanpeppers ❤️

@jonathanpeppers
Copy link
Member

If some SKSomethingView has a circular reference, I don't think dotnet/maui#22561 will fix it.

Do you have any on-device tests, can you write one like:

And then, the solution would be to fix some of the warnings this analyzer catches:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants