Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Fixes 15049 #15236

Merged
merged 2 commits into from
Mar 21, 2022
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
@@ -0,0 +1,103 @@
using System;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.Runtime.CompilerServices;
using System.ComponentModel;

#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
[NUnit.Framework.Category(Core.UITests.UITestCategories.Github10000)]
[NUnit.Framework.Category(Core.UITests.UITestCategories.CollectionView)]
[NUnit.Framework.Category(Core.UITests.UITestCategories.ManualReview)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 15049, "[Android] CollectionView leaks GREF when items are removed", PlatformAffected.Android, issueTestNumber: 1)]
public class GitHub15049 : TestContentPage
{
protected override void Init()
{
BindingContext = new GitHub15049ViewModel();
var collectionView = new CollectionView();
collectionView.ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Vertical);
collectionView.ItemTemplate = new DataTemplate(() =>
{
var label = new Label();
label.SetBinding(Label.TextProperty, "Text");

return label;
});
collectionView.SetBinding(CollectionView.ItemsSourceProperty, "Items");
Content = collectionView;
}

public class GitHub15049ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private ObservableCollection<GitHub15049Model> _items;
public ObservableCollection<GitHub15049Model> Items
{
get => _items;
set
{
if (_items != value)
{
_items = value;
RaisePropertyChanged(nameof(Items));
}
}
}

public bool IsStopped { get; set; } = false;

public GitHub15049ViewModel()
{
var collection = new ObservableCollection<GitHub15049Model>();
var pageSize = 10000;

for (var i = 0; i < pageSize; i++)
{
collection.Add(new GitHub15049Model
{
Text = "Item " + i,
});
}

Items = collection;

//Kick off Test
Task.Run(async () =>
{
while (Items.Count > 0 && !IsStopped)
{
await Task.Yield();

await Device.InvokeOnMainThreadAsync(() =>
{
Items.RemoveAt(0);
});
}
});
}

protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

public class GitHub15049Model
{
public string Text { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)CollectionViewGroupTypeIssue.cs" />
<Compile Include="$(MSBuildThisFileDirectory)GitHub15049.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue10124.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue12603.xaml.cs">
<DependentUpon>Issue12603.xaml</DependentUpon>
Expand Down
22 changes: 11 additions & 11 deletions Xamarin.Forms.Platform.Android/CollectionView/AdapterNotifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public void NotifyItemInserted(IItemsViewSource source, int startIndex)
{
_adapter.NotifyItemInserted(startIndex);

var changedCount = _adapter.ItemCount - startIndex;
_adapter.NotifyItemRangeChanged(startIndex, changedCount);
//var changedCount = _adapter.ItemCount - startIndex;
//_adapter.NotifyItemRangeChanged(startIndex, changedCount);
}
}

Expand All @@ -41,9 +41,9 @@ public void NotifyItemMoved(IItemsViewSource source, int fromPosition, int toPos
{
_adapter.NotifyItemMoved(fromPosition, toPosition);

var minPosition = System.Math.Min(fromPosition, toPosition);
var changedCount = _adapter.ItemCount - minPosition;
_adapter.NotifyItemRangeChanged(minPosition, changedCount);
//var minPosition = System.Math.Min(fromPosition, toPosition);
//var changedCount = _adapter.ItemCount - minPosition;
//_adapter.NotifyItemRangeChanged(minPosition, changedCount);
}
}

Expand All @@ -59,8 +59,8 @@ public void NotifyItemRangeInserted(IItemsViewSource source, int startIndex, int
{
_adapter.NotifyItemRangeInserted(startIndex, count);

var changedCount = _adapter.ItemCount - startIndex;
_adapter.NotifyItemRangeChanged(startIndex, changedCount);
//var changedCount = _adapter.ItemCount - startIndex;
//_adapter.NotifyItemRangeChanged(startIndex, changedCount);
}
}

Expand All @@ -70,8 +70,8 @@ public void NotifyItemRangeRemoved(IItemsViewSource source, int startIndex, int
{
_adapter.NotifyItemRangeRemoved(startIndex, count);

var changedCount = _adapter.ItemCount - startIndex;
_adapter.NotifyItemRangeChanged(startIndex, changedCount);
//var changedCount = _adapter.ItemCount - startIndex;
//_adapter.NotifyItemRangeChanged(startIndex, changedCount);
}
}

Expand All @@ -81,8 +81,8 @@ public void NotifyItemRemoved(IItemsViewSource source, int startIndex)
{
_adapter.NotifyItemRemoved(startIndex);

var changedCount = _adapter.ItemCount - startIndex;
_adapter.NotifyItemRangeChanged(startIndex, changedCount);
//var changedCount = _adapter.ItemCount - startIndex;
//_adapter.NotifyItemRangeChanged(startIndex, changedCount);
}
}

Expand Down