-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Fix memory leaks with events. #547
Conversation
Add Start/StopListening. Add Cleanup. Add event manager for PropertyChanged and ProeprtyChanging.
Be really careful with this on WP - it seems to use For info including repro see MvvmCross/MvvmCross#623 - with our "fix" on MvvmCross/MvvmCross@f1ebeb5 - we use strong |
I have seen that issue and verified that this change does not have the same problem and works on WP8. I believe this has to do with the fact that I store the |
Conflicts: ReactiveUI.Platforms/Cocoa/ReactiveCollectionViewSource.cs
This PR makes |
It is Weird. Totally missed that this was also defined in the same class. Let me fix that. |
@paulcbetts thanks for the style fixes. I was planning on doing them :) |
This is looking good, excited to get this into GitHub for Windows - lemme verify it one more time and we'll get it merged |
Fix memory leaks with events.
Thanks @jlaanstra! This is really great. |
I agree. This solves lots of memory issues which are hard to find. I also pushed some tests to master since this was already merged. |
Sorry about that |
I am afraid this is causing issues with WPF's PropertyChangedWeakEventManager (and any other 'client' that implements WeakEvent'ing) |
@jlaanstra Why does ReactiveList need to use the event manager here? It seems like it should only be needed in the controls and view controllers. Other containers like ObservableCollection don't do this, so I'm trying to understand why ReactiveList needs to. This seems to be breaking Xamarin.Forms ListViews that bind to ReactiveList since Xamarin.Forms already uses an event manager. |
This PR adds a
WeakEventManager
class to fix all kinds of memory leak issues that the different Xaml platforms have. Since these changes could break existing applications that depend on the strong references it would be great to add this to RxUI 6 before it is released.Problem
Button
instances attach to theCanExecuteChanged
event on anICommand
instance. This causes a leak when the ViewModel exists longer than the View. Details: Memory leak using "Tibet" binding with WinStore and WPF mvvmcoss apps MvvmCross/MvvmCross#552 and http://social.msdn.microsoft.com/Forums/silverlight/en-US/34d85c3f-52ea-4adc-bb32-8297f5549042/command-binding-memory-leak?forum=silverlightbugsLongListSelector
instances attach to the CollectionChanged event on anINotifyCollectionChanged
instance. Same problem as above. Details: http://social.msdn.microsoft.com/Forums/en-US/11684ea3-5c71-4354-a4e4-c337198f52b1/wp8-longlistselector-memory-leak?forum=wpdevelopIt seems that other controls like
Pivot
have similar issues.Solution
To fix all memory leaks related to event subscription from the UI,
WeakEventManager
instances have been added.CanExecuteChanged
event ofReactiveCommand
is now handled with aCanExecuteChangedEventManager
.CollectionChang(ing|ed)
events ofReactiveList
are now handled with aCollectionChang(ing|ed)EventManager
.PropertyChang(ing|ed)
events ofIReactiveObject
are now handled with aPropertyChang(ing|ed)EventManager
.Implementation
The implementation is based on the
WeakEventManager
class in WPF and the code in http://social.msdn.microsoft.com/Forums/silverlight/en-US/34d85c3f-52ea-4adc-bb32-8297f5549042/command-binding-memory-leak?forum=silverlightbugsIt contains a
ConditionalWeakTable
for the Target <-> Delegate mapping to keep the reference to the event handler alive as long as the control itself is alive.The second
ConditionalWeakTable
exists to make sure the event manager does not leak the source of the event. This would be theICommand
instance in the case of theCanExecuteChanged
event.TODO: