Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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/Handlers/Items/iOS/TemplatedCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ protected TemplatedCell(CGRect frame) : base(frame)
{
}

internal IPlatformViewHandler PlatformHandler { get; private set; }
WeakReference<IPlatformViewHandler> _handler;

internal IPlatformViewHandler PlatformHandler
{
get => _handler is not null && _handler.TryGetTarget(out var h) ? h : null;
set => _handler = value == null ? null : new(value);
}

public override void ConstrainTo(CGSize constraint)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
Expand Down Expand Up @@ -90,5 +90,47 @@ await CreateHandlerAndAddToWindow<CollectionViewHandler>(collectionView, async h
Assert.Equal(margin, absPoint.X);
});
}

[Fact("Cells Do Not Leak")]
public async Task CellsDoNotLeak()
{
SetupBuilder();

var labels = new List<WeakReference>();
VerticalCell cell = null;

{
var bindingContext = "foo";
var collectionView = new CollectionView
{
ItemTemplate = new DataTemplate(() =>
{
var label = new Label();
labels.Add(new(label));
return label;
}),
};

var handler = await CreateHandlerAsync(collectionView);

await InvokeOnMainThreadAsync(() =>
{
cell = new VerticalCell(CGRect.Empty);
cell.Bind(collectionView.ItemTemplate, bindingContext, collectionView);
});

Assert.NotNull(cell);
Assert.NotEmpty(labels);
}

await Task.Yield();
GC.Collect();
GC.WaitForPendingFinalizers();

foreach (var reference in labels)
{
Assert.False(reference.IsAlive, "View should not be alive!");
}
}
}
}