Bug 29419 - Setting RefreshCommand cause leak
Summary: Setting RefreshCommand cause leak
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.4.2
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2015-04-27 08:03 UTC by Kasper
Modified: 2017-08-11 16:58 UTC (History)
7 users (show)

Tags: ac
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Kasper 2015-04-27 08:03:32 UTC
If c implements ICommand, then setting ListView.RefreshCommand = c will subscribe to c.CanExecuteChanged in order to prevent the command c from being called when that is not allowed. So far so good.

The problem is that XF does not unsubscribe from the event, when the page containing the ListView is pop'ed from the navigation stack. Therefor the ListView can not be reclaimed by the GC until the command is GC'ed. If the command c is long lived (as is typical with commands living in the ViewModel layer of MVVM), then that may not happen until the process exits.

I have observed this behaviour on Android with XF 1.4.2, but I suspect it is a problem on all platforms.

As a work-around, don't set the ListView.RefreshCommand. Instead subscribe to ListView.Refreshing event and handle the command invocation by your self.
Comment 1 Eric Maupin 2015-05-21 18:56:34 UTC
When the RefreshCommand is set to null, we do unsubscribe for the event, so that's an easier work around for you.
Comment 2 Kasper 2015-05-22 04:44:40 UTC
But when should I set the RefreshCommand to null? When the user press the back button, the page is pop'ed outside my code. Which event should I react to?

My workaround avoid this issue all together by never subscribing to ICommand.CanExecuteChanged. I can always check in the ListView.Refreshing event, if the command can execute or not.

Obviously I would prefer for XF to automatically unsubscribe from ICommand.CanExecuteChanged when the view is not visible.
Comment 3 adrianknight89 2016-12-07 22:14:13 UTC
I can confirm this. 

However, I have tried to reproduce the memory leak on top of https://github.com/xamarin/Xamarin.Forms/pull/524 but wasn't able to. I'll try again when it's merged.
Comment 4 Jimmy [MSFT] 2017-08-11 16:58:01 UTC
We are unable to reproduce this issue with the information given. If you are still experiencing this issue with Xamarin.Forms 2.3.4+ and when using FormsAppCompat in your Android project, please file a new bug report and provide a repro project so we can investigate further. Thanks!