I see Messenger
uses WeakReference
to store references to event emitters and event listeners:
https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Core/MessagingCenter.cs
Is calling Unscribe
still necessary to avoid memory leaks?
I think I hit a situation on Android where it really looks like I must call it, otherwise I have a memory leak.
To check for a potential memory leak, I added the deconstructor to both view-model and view and I call GC.Collect
on OnDisappearing
on the Page
, and the deconstructors do not get called unless I call Unscribe
.
EDIT: After a closer inspection I see Messenger
also stores the Action
callback passed to Messenger.Subscribe()
, but without using a WeakReference
!
See https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Core/MessagingCenter.cs#L107
I am confused, why isn't it using a WeakReference
also for storing the Action
!? Because it's obvious it's going to leak unless I call Unscribe