xamarin / xamarin-macios

.NET for iOS, Mac Catalyst, macOS, and tvOS provide open-source bindings of the Apple SDKs for use with .NET managed languages such as C#
Other
2.44k stars 507 forks source link

SkiaSharp on iOS gets corrupted after opening and hiding many pages, grids and layouts #20915

Open janne-hmp opened 1 month ago

janne-hmp commented 1 month ago

I'm working on a game called GnollHack in .NET MAUI 9.0 Preview 6 that uses SkiaSharp 3.0 Preview 3.1 with several SKCanvasViews. However, after opening and hiding several pages or grids covering the whole page, SkiaSharp somehow gets corrupted and starts drawing just black. It may be a memory issue, runtime issue, composition engine issue, or something else.

In Xamarin iOS using SkiaSharp 2.88.8, the same pages and grids worked just fine. They also work with SkiaSharp 3.0 Preview 3.1 on Android and Windows. So this seems to be a .NET MAUI problem on iOS, perhaps something with .NET runtime on iOS or the like.

I have already posted this to SkiaSharp repository many months ago, but there have been no reaction to it, and the problem persists in .NET MAUI 9.0 Preview 6. This really prevents any serious use of SkiaSharp on iOS in .NET MAUI, and in consequence the publication of our game on iOS as well, so it is a pretty critical issue overall.

Does anybody know any settings for the project file or workarounds I could try here?

Steps to Reproduce

  1. Download the test app at https://github.com/hyvanmielenpelit/iOSMauiSkiaSharpMemoryLeak
  2. Press click me, which start's opening and closing the same page up to 500 times
  3. The app crashes after it runs out of memory. My old iPad (11,7) crashed after 145th time.

Expected Behavior

SkiaSharp works just as in Xamarin iOS in .NET MAUI.

Actual Behavior

All SkiaSharp canvases start drawing black after a few clicks of Play Game.

Environment

iPad 11,7. iOS 17.5.1

Microsoft Visual Studio Community 2022 Version 17.11.0 Preview 4.0 VisualStudio.17.Preview/17.11.0-pre.4.0+35111.106 Microsoft .NET Framework Version 4.8.09032

Installed Version: Community

Visual C++ 2022 00482-90000-00000-AA350 Microsoft Visual C++ 2022

ADL Tools Service Provider 1.0 This package contains services used by Data Lake tools

ASA Service Provider 1.0

ASP.NET and Web Tools 17.11.230.51983 ASP.NET and Web Tools

Azure App Service Tools v3.0.0 17.11.230.51983 Azure App Service Tools v3.0.0

Azure Data Lake Tools for Visual Studio 2.6.5000.0 Microsoft Azure Data Lake Tools for Visual Studio

Azure Functions and Web Jobs Tools 17.11.230.51983 Azure Functions and Web Jobs Tools

Azure Stream Analytics Tools for Visual Studio 2.6.5000.0 Microsoft Azure Stream Analytics Tools for Visual Studio

C# Tools 4.11.0-3.24358.9+26240cf3e46e45a9fc8c879f793793defe2c0b2a C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Extensibility Message Bus 1.4.39 (main@e8108eb) Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

GitHub Copilot 0.2.1589.24474 GitHub Copilot is an AI pair programmer that helps you write code faster and with less work.

Linux Core Dump Debugging 1.0.9.35103 Enables debugging of Linux core dumps.

Microsoft Azure Hive Query Language Service 2.6.5000.0 Language service for Hive query

Microsoft Azure Stream Analytics Language Service 2.6.5000.0 Language service for Azure Stream Analytics

Microsoft Azure Tools for Visual Studio 2.9 Support for Azure Cloud Services projects

Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Mono Debugging for Visual Studio 17.11.3 (ba13144) Support for debugging Mono processes with Visual Studio.

NuGet Package Manager 6.11.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

Razor (ASP.NET Core) 17.11.3.2432707+ea83da42ade2a5e193507d780559d79f0cb16c75 Provides languages services for ASP.NET Core Razor.

SQL Server Data Tools 17.11.38.0 Microsoft SQL Server Data Tools

Test Adapter for Boost.Test 1.0 Enables Visual Studio's testing tools with unit tests written for Boost.Test. The use terms and Third Party Notices are available in the extension installation directory.

Test Adapter for Google Test 1.0 Enables Visual Studio's testing tools with unit tests written for Google Test. The use terms and Third Party Notices are available in the extension installation directory.

ToolWindowHostedEditor 1.0 Hosting json editor into a tool window

TypeScript Tools 17.0.30708.2002 TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools 4.11.0-3.24358.9+26240cf3e46e45a9fc8c879f793793defe2c0b2a Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual C++ for Cross Platform Mobile Development (Android) 15.0.35103.136 Visual C++ for Cross Platform Mobile Development (Android)

Visual C++ for Cross Platform Mobile Development (iOS) 15.0.35103.136 Visual C++ for Cross Platform Mobile Development (iOS)

Visual C++ for Linux Development 1.0.9.35103 Visual C++ for Linux Development

Visual F# Tools 17.11.0-beta.24317.3+c07c8ebad62f6629b36349381991a299598a8d74 Microsoft Visual F# Tools

Visual Studio IntelliCode 2.2 AI-assisted development for Visual Studio.

VisualStudio.DeviceLog 1.0 Information about my package

VisualStudio.Mac 1.0 Mac Extension for Visual Studio

VSPackage Extension 1.0 VSPackage Visual Studio Extension Detailed Info

Xamarin 17.11.0.86 (main@9d38808) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer 17.11.3.10 (remotes/origin/d17-11@714146c62b) Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates 17.9.0 (38e87ba) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK 13.2.2.0 (d17-5/45b0e14) Xamarin.Android Reference Assemblies and MSBuild support. Mono: d9a6e87 Java.Interop: xamarin/java.interop/d17-5@149d70fe SQLite: xamarin/sqlite/3.40.1@68c69d8 Xamarin.Android Tools: xamarin/xamarin-android-tools/d17-5@ca1552d

Xamarin.iOS and Xamarin.Mac SDK 16.4.0.23 (9defd91b3) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

Build Logs

Available upon request, but everything builds just fine.

Example Project (If Possible)

https://github.com/hyvanmielenpelit/iOSMauiSkiaSharpMemoryLeak

janne-hmp commented 1 month ago

I just made a new example project here: https://github.com/hyvanmielenpelit/iOSMauiSkiaSharpMemoryLeak . It tries to open and close the same page 500 times, but crashes after 145th time or so.

I will also update the original description accordingly.

rolfbjarne commented 1 month ago

If I add a GC.Collect between each iteration, the memory usage seems to stay stable. Can you try that and see if something changes on your end? I'm not suggesting this as something you should actually do in the app, it's just to help diagnose the problem.

Unfortunately I haven't been able to use memory profiling with Instruments on your app, for some reason the code Instruments injects in the app to profile memory ends up crashing the app. Would it be possible to create a test project without MAUI to see if Instruments likes that better, or does your code require MAUI?

janne-hmp commented 1 month ago

Actually, Matt Leibowitz has most likely already fixed this in the forthcoming version of SkiaSharp. The problem on iOS was that there were circular references between PlatformView and its Maui control, preventing garbage collection on iOS. Also Jonathan Peppers knows more. I've been waiting for the new version to come out before closing this issue.

rolfbjarne commented 1 month ago

Unfortunately I haven't been able to use memory profiling with Instruments on your app, for some reason the code Instruments injects in the app to profile memory ends up crashing the app. Would it be possible to create a test project without MAUI to see if Instruments likes that better, or does your code require MAUI?

Actually nvm about this part, I disabled the interpreter and now Instruments work