Open jonathanpeppers opened 2 weeks ago
Fixes: https://github.com/dotnet/maui/issues/20195 Context: https://github.com/AdamEssenmacher/MemoryToolkit.Maui/tree/main/samples
Retesting this sample with the ListView leak fixed, I noticed the sample reports 0 leaks! Unfortunately, it still displayed memory growing over time...
ListView
Taking a .gcdump snapshot, I noticed a cycle:
.gcdump
ViewCellRenderer.ViewTableCell
ViewCell _viewCell
ViewCellRenderer
ViewTableCell
I was able to write a test that reproduces the leak, and I extended it for every type of *Cell like TextCell, ImageCell, SwitchCell, etc.
*Cell
TextCell
ImageCell
SwitchCell
The fixes are:
ViewTableCell now uses a WeakReference<ViewCell> _viewCell
WeakReference<ViewCell> _viewCell
CellTableViewCell now uses a WeakReference<Cell> _cell
CellTableViewCell
WeakReference<Cell> _cell
CellTableViewCell now uses a WeakEventManager for PropertyChanged, as the *Renderer subscribes to this event.
WeakEventManager
PropertyChanged
*Renderer
Note that I changed PropertyChanged to an event, which is an API change. (I can revisit this if needed)
Needs a rebase to get the updated snapshot for Issue18242Test and fix the build.
Fixes: https://github.com/dotnet/maui/issues/20195 Context: https://github.com/AdamEssenmacher/MemoryToolkit.Maui/tree/main/samples
Retesting this sample with the
ListView
leak fixed, I noticed the sample reports 0 leaks! Unfortunately, it still displayed memory growing over time...Taking a
.gcdump
snapshot, I noticed a cycle:ViewCellRenderer.ViewTableCell
->ViewCell _viewCell
->ViewCellRenderer
->ViewTableCell
I was able to write a test that reproduces the leak, and I extended it for every type of
*Cell
likeTextCell
,ImageCell
,SwitchCell
, etc.The fixes are:
ViewTableCell
now uses aWeakReference<ViewCell> _viewCell
CellTableViewCell
now uses aWeakReference<Cell> _cell
CellTableViewCell
now uses aWeakEventManager
forPropertyChanged
, as the*Renderer
subscribes to this event.Note that I changed
PropertyChanged
to an event, which is an API change. (I can revisit this if needed)