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

[ios] fix leak in ListView *Cells #23143

Merged
merged 6 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Controls/src/Core/Cells/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,14 @@ void OnParentPropertyChanging(object sender, PropertyChangingEventArgs e)
internal Android.Views.View ConvertView { get; set; }
#elif IOS
internal UIKit.UITableViewCell ReusableCell { get; set; }
internal UIKit.UITableView TableView { get; set; }

WeakReference<UIKit.UITableView> _tableView;

internal UIKit.UITableView TableView
{
get => _tableView?.GetTargetOrDefault();
set => _tableView = value is null ? null : new(value);
}
#endif


Expand Down
34 changes: 30 additions & 4 deletions src/Controls/tests/DeviceTests/Elements/ListView/ListViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,17 @@ await CreateHandlerAndAddToWindow<LayoutHandler>(layout, async _ =>
});
}

[Fact("Cells Do Not Leak"
#if !WINDOWS
[Theory("Cells Do Not Leak"
#if !WINDOWS && !IOS
, Skip = "Skip for now on other platforms, due to how cells are recycled this does not pass."
#endif
)]
public async Task CellsDoNotLeak()
[InlineData(typeof(TextCell))]
[InlineData(typeof(EntryCell))]
[InlineData(typeof(ImageCell))]
[InlineData(typeof(SwitchCell))]
[InlineData(typeof(ViewCell))]
public async Task CellsDoNotLeak(Type type)
{
SetupBuilder();

Expand All @@ -288,7 +293,11 @@ public async Task CellsDoNotLeak()
{
ItemTemplate = new DataTemplate(() =>
{
var cell = new TextCell();
var cell = (Cell)Activator.CreateInstance(type);
if (cell is ViewCell viewCell)
{
viewCell.View = new Label();
}
references.Add(new(cell));
return cell;
})
Expand All @@ -299,6 +308,23 @@ await CreateHandlerAndAddToWindow<ListViewRenderer>(listView, async _ =>
listView.ItemsSource = new[] { 1, 2, 3 };
await Task.Delay(100);
ValidatePlatformCells(listView);

Assert.NotEmpty(references);
foreach (var reference in references.ToArray())
{
if (reference.Target is Cell cell)
{
Assert.NotNull(cell.Handler);
references.Add(new(cell.Handler));
Assert.NotNull(cell.Handler.PlatformView);
references.Add(new(cell.Handler.PlatformView));
#if IOS
Assert.NotNull(cell.TableView);
references.Add(new(cell.TableView));
#endif
}
}

listView.ItemsSource = null;
await Task.Delay(100);
ValidatePlatformCells(listView);
Expand Down
Loading