-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
[Windows] GroupedItemTemplateCollection CollectionChanged event not unsubscribed, causes memory leak. #22954
Comments
Hi I'm an AI powered bot that finds similar issues based off the issue title. Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you! Open similar issues:
Closed similar issues:
|
Can you test with the latest nightly build? |
@PureWeen I have installed the 8.0.60-ci.net8.24309.1 nightly build: As you can see in the screenshot, it still keeps a lot of references to the |
Added repro repo |
Verified this issue with Visual Studio 17.11.0 Preview 2.0 (8.0.60 & 8.0.40& 8.0.3). Can repro on Windows platform with sample project. |
Any update on this? This basically makes grouped collections unusable on Windows platform :/ |
@PureWeen any estimation when this can be resolved? |
Description
CollectionView & ObservableCollections
If you use
CollectionView
withIsGrouped=true
where yourItemsSource
is of typeObservableCollection
; congratulations you just introduced a memory leak in your application! Don't worry: the fix is easy! If it ever gets picked up by the MAUI team ;)If you use grouping, the internal source for
CollectionViewSource
will be of typemaui/src/Controls/src/Core/Platform/Windows/CollectionView/GroupedItemTemplateCollection.cs
Line 9 in b182ffe
maui/src/Controls/src/Core/Handlers/Items/GroupableItemsViewHandler.Windows.cs
Line 19 in b182ffe
Inside the
GroupedItemTemplateCollection
there is a check to see if the itemssource implementsINotifyCollectionChanged
:Maybe you already feel it coming or not... But yes indeed this event is never unsubscribed + the
GroupsChanged
event is not a static method. Which according to their docs does introduce a leak: https://github.com/dotnet/maui/wiki/Memory-Leaks#when-we-cant-use-weakeventmanagerI have spent many, way too many, hours testing, analyzing MAUI source code, verifying and more testing to confirm this is the root cause of the issue.
Test setup
I have a basic mainpage with a viewmodel. In the viewmodel I open a popup with a
CollectionView
as the content of the popup.I have my own sub class of a
CollectionView
with adestructor
added so I can verify if it is cleaned up or not (it is not).Memory Usage/Snapshot & analysis
What I noticed is that the
Count
of theGroupedItemTemplateCollection
is always 7x theCount
of theCollectionView
.I guess it has to do with
maui/src/Controls/src/Core/Handlers/Items/GroupableItemsViewHandler.cs
Line 20 in b182ffe
UpdateItemsSource
multiple times. In here theCleanUpCollectionViewSource
is executed:maui/src/Controls/src/Core/Handlers/Items/ItemsViewHandler.Windows.cs
Line 156 in b182ffe
My idea is that the
GroupableItemsViewHandler
should override theCleanUpCollectionViewSource
and check if thesource
isGroupedItemTemplateCollection
. Since this handler is also responsible for creating this type of collection. Then do aCleanUp
for example:However, since this is not happening at the moment. Every time the
UpdateItemsSource
is triggered, a new memory leak is introduced.The solution
Either make the
GroupsChanged
static or follow https://github.com/dotnet/maui/wiki/Memory-Leaks#when-we-cant-use-weakeventmanagerI hope people can follow my explanation, I know I am not the best with explaining such things. Ill publish a repro repo soon, together with the workaround... well "workaround"
Steps to Reproduce
CollectionView
withIsGrouped=true
ItemsSource
binding is set to anObservableCollection
Link to public reproduction project repository
https://github.com/jari-schuurman/GroupFooterTemplateIssue
Version with bug
8.0.3 GA
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
Windows
Affected platform versions
windows10.0.19401.0
Did you find any workaround?
In order to fix it temporarily, you will have to do some magic:
CollectionView
:GroupedCollection
. You do not need to add any custom properties.Handler
for this control that inherit fromCollectionViewHandler
, e.g.:GroupedCollectionHandler
Handler
to your maui program:handlers.AddHandler(typeof(GroupedCollection), typeof(GroupedCollectionHandler));
All those handlers and classes are
internal
so we cannot easily change them:Yes I know for now the method name check is hardcoded. Could probably make it more clean, but this is just POC.
Basically we keep track of all the handlers being added, then in our
CollectionViewHandler
we check if the type of theitemssource
is ofICleanObservableCollection
:I didn't take the time to see if we can make this check more performant, but for now it works :)
6. Use the
CleanObservableCollection
for yourItemsSource
binding and use the custom control created in step1
instead of theCollectionView
7. NOTE: THIS IS MEANT FOR WINDOWS. IF you do cross platform, you probably want to wrap the
addHandler
step with the #if WINDOWS #endif tags8. Confirm destructor is called & memory is stable
9. Please maui team solve this quickly. I do not want to add such code in my production repo, but now I am forced to do so
Relevant log output
No response
The text was updated successfully, but these errors were encountered: