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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[windows] fix memory leak in ListView #16762

Merged
merged 3 commits into from
Aug 17, 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 @@ -69,6 +69,8 @@ void OnUnloaded(object sender, RoutedEventArgs e)
Cell.SendDisappearing();
// 馃殌 unsubscribe from propertychanged
Cell.PropertyChanged -= _propertyChangedHandler;
// Allows the Cell to unsubscribe from Parent.PropertyChanged
Cell.Parent = null;
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
锘縰sing System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
Expand Down Expand Up @@ -272,5 +273,82 @@ public async Task NullTemplateDoesntCrash()
ValidatePlatformCells(listView);
});
}

[Fact("Cells Do Not Leak"
#if !WINDOWS
, Skip = "Skip for now on other platforms, due to how cells are recycled this does not pass."
#endif
)]
public async Task CellsDoNotLeak()
{
SetupBuilder();

var references = new List<WeakReference>();
var listView = new ListView
{
ItemTemplate = new DataTemplate(() =>
{
var cell = new TextCell();
references.Add(new(cell));
return cell;
})
};

await CreateHandlerAndAddToWindow<ListViewRenderer>(listView, async _ =>
{
listView.ItemsSource = new[] { 1, 2, 3 };
await Task.Delay(100);
ValidatePlatformCells(listView);
listView.ItemsSource = null;
await Task.Delay(100);
ValidatePlatformCells(listView);
});

await AssertionExtensions.WaitForGC(references.ToArray());
foreach (var reference in references)
{
Assert.False(reference.IsAlive, "Cell should not be alive!");
}
}

[Fact("Cells Repopulate After Null ItemsSource")]
public async Task CellsRepopulateAfterNullItemsSource()
{
SetupBuilder();

List<TextCell> cells = null;

var listView = new ListView
{
ItemTemplate = new DataTemplate(() =>
{
var cell = new TextCell();
cell.SetBinding(TextCell.TextProperty, new Binding("."));
cells?.Add(cell);
return cell;
})
};

await CreateHandlerAndAddToWindow<ListViewRenderer>(listView, async _ =>
{
listView.ItemsSource = new[] { 1, 2, 3 };
await Task.Delay(100);
ValidatePlatformCells(listView);
listView.ItemsSource = null;
await Task.Delay(100);
ValidatePlatformCells(listView);

// Now track the new cells
cells = new();
listView.ItemsSource = new[] { 4, 5, 6 };
await Task.Delay(100);
ValidatePlatformCells(listView);

Assert.Equal(3, cells.Count);
Assert.Equal("4", cells[0].Text);
Assert.Equal("5", cells[1].Text);
Assert.Equal("6", cells[2].Text);
});
}
}
}
2 changes: 2 additions & 0 deletions src/Controls/tests/DeviceTests/Memory/MemoryTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
锘縰sing System;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Handlers.Compatibility;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Hosting;
using Xunit;
Expand All @@ -22,6 +23,7 @@ void SetupBuilder()
handlers.AddHandler<Editor, EditorHandler>();
handlers.AddHandler<GraphicsView, GraphicsViewHandler>();
handlers.AddHandler<Label, LabelHandler>();
handlers.AddHandler<ListView, ListViewRenderer>();
handlers.AddHandler<Picker, PickerHandler>();
handlers.AddHandler<IContentView, ContentViewHandler>();
handlers.AddHandler<Image, ImageHandler>();
Expand Down
Loading