Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[windows] fix memory leak when
CollectionView.ItemsSource
changes (#…
…13530) Fixes: #13393 Context: https://github.com/ivan-todorov-progress/maui-collection-view-memory-leak-bug Debugging the above sample, I found that `ItemsView._logicalChildren` grew indefinitely in size if you replace a `CollectionView`'s `ItemsSource` over and over. I could fix this by creating a new `internal` `ItemsView.ClearLogicalChildren()` method and call it from the Windows `ItemsViewHandler`. I could reproduce this issue in a Device Test running on Windows. It appears the problem does not occur on Android or iOS due to the recycling behavior of the underlying platforms. For example, Android calls `OnViewRecycled()` which calls `RemoveLogicalChild(view)`: * https://github.com/dotnet/maui/blob/53f6e393750a3df05b12fcde442daf3616c216f8/src/Controls/src/Core/Handlers/Items/Android/Adapters/ItemsViewAdapter.cs#L52-L57 * https://github.com/dotnet/maui/blob/53f6e393750a3df05b12fcde442daf3616c216f8/src/Controls/src/Core/Handlers/Items/Android/TemplatedItemViewHolder.cs#L37-L45 On Windows, we merely have a `ItemContentControl` and there is no callback for when things are recycled. We *do* get a callback for when the `FormsDataContext` value changes, so an option is to clear the list in this case. After this change, my test passes -- but it was already passing on iOS and Android.
- Loading branch information