Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ios/catalyst] fix memory leak with CollectionView (#21850)
Fixes: #20710 Context: https://github.com/marco-skizza/MauiCollectionView Testing the sample above, you can see a memory leak when setting up a `CollectionView`'s `DataTemplate` in XAML, but it appears to work just fine with C#. Using `dotnet-gcdump` with a `Release` build of the app, I see:  You can cause the C# version to leak, if you make the lambda an instance method: * jonathanpeppers/iOSMauiCollectionView@3e5fb02 XAML just *happens* to use an instance method, no matter if XamlC is on/off. I was able to reproduce this in `CollectionViewTests.iOS.cs` by making the `CollectionView` look like a "user control" and create an instance method. There is a "cycle" that causes the problem here: * `Microsoft.Maui.Controls.Handlers.Items.VerticalCell` (note this is a `UICollectionViewCell`) -> * `DataTemplate` -> * `Func<object>` -> * `PageXamlLeak` (or `PageCsOk` w/ my change) -> * `CollectionView` -> * `Microsoft.Maui.Controls.Handlers.Items.VerticalCell` The solution being to make `TemplatedCell` (which is a subclass of `VerticalCell`) hold only a `WeakReference` to the `DataTemplate`. With this change in place, the test passes. ~~ Notes about Catalyst ~~ 1. The new test passes on Catalyst (with the `#if` removed), if you run it by itself 2. If you run *all* the tests, it seems like there is a `Window` -> `Page` -> `CollectionView` that makes the test fail. 3. This seems like it's all related to the test setup, and not a bug. It seems like what is here might be OK for now? If Catalyst leaks, it would probably leak on iOS as well and the test passes there.
- Loading branch information