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
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ protected object GetItemAtIndex(NSIndexPath index)
return ItemsSource[index];
}

[UnconditionalSuppressMessage("Memory", "MEM0003", Justification = "Proven safe in test: MemoryTests.HandlerDoesNotLeak")]
[UnconditionalSuppressMessage("Memory", "MEM0003", Justification = "Proven safe in test: CollectionViewTests.ItemsSourceDoesNotLeak")]
void CellContentSizeChanged(object sender, EventArgs e)
{
if (_disposed)
Expand All @@ -400,7 +400,7 @@ void CellContentSizeChanged(object sender, EventArgs e)
}
}

[UnconditionalSuppressMessage("Memory", "MEM0003", Justification = "Proven safe in test: MemoryTests.HandlerDoesNotLeak")]
[UnconditionalSuppressMessage("Memory", "MEM0003", Justification = "Proven safe in test: CollectionViewTests.ItemsSourceDoesNotLeak")]
void CellLayoutAttributesChanged(object sender, LayoutAttributesChangedEventArgs args)
{
CacheCellAttributes(args.NewAttributes.IndexPath, args.NewAttributes.Size);
Expand Down
19 changes: 15 additions & 4 deletions src/Controls/src/Core/Handlers/Items/iOS/TemplatedCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@ namespace Microsoft.Maui.Controls.Handlers.Items
{
public abstract class TemplatedCell : ItemsViewCell
{
public event EventHandler<EventArgs> ContentSizeChanged;
public event EventHandler<LayoutAttributesChangedEventArgs> LayoutAttributesChanged;
Comment thread
jonathanpeppers marked this conversation as resolved.
readonly WeakEventManager _weakEventManager = new();

public event EventHandler<EventArgs> ContentSizeChanged
{
add => _weakEventManager.AddEventHandler(value);
remove => _weakEventManager.RemoveEventHandler(value);
}

public event EventHandler<LayoutAttributesChangedEventArgs> LayoutAttributesChanged
{
add => _weakEventManager.AddEventHandler(value);
remove => _weakEventManager.RemoveEventHandler(value);
}

protected CGSize ConstrainedSize;

Expand Down Expand Up @@ -287,12 +298,12 @@ void MeasureInvalidated(object sender, EventArgs args)

protected void OnContentSizeChanged()
{
ContentSizeChanged?.Invoke(this, EventArgs.Empty);
_weakEventManager.HandleEvent(this, EventArgs.Empty, nameof(ContentSizeChanged));
}

protected void OnLayoutAttributesChanged(UICollectionViewLayoutAttributes newAttributes)
{
LayoutAttributesChanged?.Invoke(this, new LayoutAttributesChangedEventArgs(newAttributes));
_weakEventManager.HandleEvent(this, new LayoutAttributesChangedEventArgs(newAttributes), nameof(LayoutAttributesChanged));
}

protected abstract bool AttributesConsistentWithConstrainedDimension(UICollectionViewLayoutAttributes attributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Handlers.Compatibility;
using Microsoft.Maui.Controls.Handlers.Items;
using Microsoft.Maui.Controls.Platform;
using Microsoft.Maui.DeviceTests.Stubs;
Expand Down Expand Up @@ -41,7 +42,11 @@ void SetupBuilder()
handlers.AddHandler<Button, ButtonHandler>();
handlers.AddHandler<SwipeView, SwipeViewHandler>();
handlers.AddHandler<SwipeItem, SwipeItemMenuItemHandler>();

#if IOS || MACCATALYST
handlers.AddHandler(typeof(NavigationPage), typeof(NavigationRenderer));
#else
handlers.AddHandler(typeof(NavigationPage), typeof(NavigationViewHandler));
#endif
#if IOS && !MACCATALYST
handlers.AddHandler<CacheTestCollectionView, CacheTestCollectionViewHandler>();
#endif
Expand Down Expand Up @@ -102,40 +107,64 @@ public async Task ItemsSourceDoesNotLeak()
{
SetupBuilder();

IList logicalChildren = null;
WeakReference weakReference = null;
var collectionView = new CollectionView
{
Header = new Label { Text = "Header" },
Footer = new Label { Text = "Footer" },
ItemTemplate = new DataTemplate(() => new Label())
};
var weakReferences = new List<WeakReference>();

await CreateHandlerAndAddToWindow<CollectionViewHandler>(collectionView, async handler =>
{
var data = new ObservableCollection<string>()
var labels = new List<Label>();
IList logicalChildren = null;
var collectionView = new CollectionView
{
"Item 1",
"Item 2",
"Item 3"
Header = new Label { Text = "Header" },
Footer = new Label { Text = "Footer" },
ItemTemplate = new DataTemplate(() =>
{
var label = new Label();
labels.Add(label);
return label;
}),
};
weakReference = new WeakReference(data);
collectionView.ItemsSource = data;
await Task.Delay(100);

// Get ItemsView._logicalChildren
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
logicalChildren = typeof(Element).GetField("_internalChildren", flags).GetValue(collectionView) as IList;
Assert.NotNull(logicalChildren);
var navPage = new NavigationPage(new ContentPage { Title = "Page 1" });

// Replace with cloned collection
collectionView.ItemsSource = new ObservableCollection<string>(data);
await Task.Delay(100);
});
await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), async handler =>
{
await navPage.PushAsync(new ContentPage { Content = collectionView });

var data = new ObservableCollection<string>()
{
"Item 1",
"Item 2",
"Item 3"
};
weakReferences.Add(new(data));
collectionView.ItemsSource = data;
await Task.Delay(100);

Assert.NotEmpty(labels);
foreach (var label in labels)
{
weakReferences.Add(new(label));
weakReferences.Add(new(label.Handler));
weakReferences.Add(new(label.Handler.PlatformView));
}

// Get ItemsView._logicalChildren
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
logicalChildren = typeof(Element).GetField("_internalChildren", flags).GetValue(collectionView) as IList;
Assert.NotNull(logicalChildren);

// Replace with cloned collection
collectionView.ItemsSource = new ObservableCollection<string>(data);
await Task.Delay(100);
await navPage.PopAsync();
});


Assert.NotNull(logicalChildren);
Assert.True(logicalChildren.Count <= 5, "_logicalChildren should not grow in size!");
}

await AssertionExtensions.WaitForGC(weakReference);
Assert.NotNull(logicalChildren);
Assert.True(logicalChildren.Count <= 5, "_logicalChildren should not grow in size!");
await AssertionExtensions.WaitForGC([.. weakReferences]);
}

[Theory]
Expand Down