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

[windows] fix memory leak when CollectionView.ItemsSource changes #13530

Merged
merged 3 commits into from
Feb 27, 2023
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 @@ -123,6 +123,7 @@ protected virtual void CleanUpCollectionViewSource()
CollectionViewSource.Source = null;
CollectionViewSource = null;
}
VirtualView?.ClearLogicalChildren();

if (VirtualView?.ItemsSource == null)
{
Expand Down
9 changes: 9 additions & 0 deletions src/Controls/src/Core/Items/ItemsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ public void RemoveLogicalChild(Element element)
VisualDiagnostics.OnChildRemoved(this, element, oldLogicalIndex);
}

internal void ClearLogicalChildren()
{
// Reverse for-loop, so children can be removed while iterating
for (int i = _logicalChildren.Count - 1; i >= 0; i--)
{
RemoveLogicalChild(_logicalChildren[i]);
}
}

internal override IReadOnlyList<Element> LogicalChildrenInternal => _logicalChildren.AsReadOnly();

internal static readonly BindableProperty InternalItemsLayoutProperty =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using Microsoft.Maui.Controls;
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Handlers.Items;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Handlers;
Expand Down Expand Up @@ -28,5 +33,49 @@ void SetupBuilder()
});
});
}

[Fact]
public async Task ItemsSourceDoesNotLeak()
{
SetupBuilder();

IList logicalChildren = null;
WeakReference weakReference = null;
var collectionView = new CollectionView
{
ItemTemplate = new DataTemplate(() => new Label())
};

await CreateHandlerAndAddToWindow<CollectionViewHandler>(collectionView, async handler =>
{
var data = new ObservableCollection<string>()
{
"Item 1",
"Item 2",
"Item 3"
};
weakReference = new WeakReference(data);
collectionView.ItemsSource = data;
await Task.Delay(100);

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

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

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

Assert.NotNull(weakReference);
Assert.False(weakReference.IsAlive, "ObservableCollection should not be alive!");
Assert.NotNull(logicalChildren);
Assert.True(logicalChildren.Count <= 3, "_logicalChildren should not grow in size!");
}
}
}