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

[controls] fix memory leak with Grid Row/ColumnDefinitions #16145

Merged
merged 1 commit into from
Jul 18, 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
11 changes: 7 additions & 4 deletions src/Controls/src/Core/DefinitionCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Microsoft.Maui.Controls
{
public class DefinitionCollection<T> : IList<T>, ICollection<T> where T : IDefinition
{
readonly WeakEventManager _weakEventManager = new WeakEventManager();
readonly List<T> _internalList;

internal DefinitionCollection() => _internalList = new List<T>();
Expand Down Expand Up @@ -101,13 +102,15 @@ public void RemoveAt(int index)
OnItemSizeChanged(this, EventArgs.Empty);
}

public event EventHandler ItemSizeChanged;
public event EventHandler ItemSizeChanged
{
add => _weakEventManager.AddEventHandler(value);
remove => _weakEventManager.RemoveEventHandler(value);
}
Comment on lines -104 to +109
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some existing tests that check this event still fires:

[Fact]
public void ChangingChildRowInvalidatesGrid()

[Fact]
public void ChangingChildColumnInvalidatesGrid()


void OnItemSizeChanged(object sender, EventArgs e)
{
EventHandler eh = ItemSizeChanged;
if (eh != null)
eh(this, EventArgs.Empty);
_weakEventManager.HandleEvent(this, e, nameof(ItemSizeChanged));
}
}
}
44 changes: 43 additions & 1 deletion src/Controls/tests/Core.UnitTests/Layouts/GridLayoutTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NSubstitute;
using Xunit;

Expand Down Expand Up @@ -184,5 +186,45 @@ public void ColumnDefinitionsGetBindingContext()

Assert.Equal(def2.BindingContext, context);
}

[Fact]
public async Task ColumnDefinitionDoesNotLeak()
{
// Long-lived column, like from a Style in App.Resources
var column = new ColumnDefinition();
WeakReference reference;

{
rachelkang marked this conversation as resolved.
Show resolved Hide resolved
var grid = new Grid();
grid.ColumnDefinitions.Add(column);
reference = new(grid);
}

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

Assert.False(reference.IsAlive, "Grid should not be alive!");
rachelkang marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
public async Task RowDefinitionDoesNotLeak()
{
// Long-lived row, like from a Style in App.Resources
var row = new RowDefinition();
WeakReference reference;

{
rachelkang marked this conversation as resolved.
Show resolved Hide resolved
var grid = new Grid();
grid.RowDefinitions.Add(row);
reference = new(grid);
}

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

Assert.False(reference.IsAlive, "Grid should not be alive!");
}
}
}