This is Xamarin's bug tracking system. For product support, please use the support links listed in your Xamarin Account.
Bug 44940 - ScrollToAsync memory leak on UWP
Summary: ScrollToAsync memory leak on UWP
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms (show other bugs)
Version: 2.3.1
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-09-30 14:02 UTC by John Hardman
Modified: 2017-02-17 10:49 UTC (History)
5 users (show)

See Also:
Tags: ScrollView ScrollToAsync Memory Leak UWP ac
Is this bug a regression?: ---
Last known good build:


Attachments

Description John Hardman 2016-09-30 14:02:14 UTC
Using XF 2.3.1.114 on UWP, a memory leak occurs when using ScrollView.ScrollToAsync.
The code below works, without leaking memory, on Android and iOS.
However, on UWP, after ScrollToAsync is executed, the page is never cleaned up by the garbage collector. Something within ScrollToAsync is holding a reference even after the page is popped.



        private object _lockObject; // protects _donateGrid and _scrollView
        private volatile Grid _donateGrid; // protected by _lockObject
        private bool _scrollNotYetDone = true;
        private volatile ScrollView _scrollView; // protected by _lockObject

        // The above are initalised elsewhere

        protected override async void OnSizeAllocated(double width, double height)
        {
            base.OnSizeAllocated(width, height);

            ScrollView scrollView;
            Grid donateGrid;
            lock (_lockObject)
            {
                scrollView = _scrollView;
                donateGrid = _donateGrid;
            };

            if ((scrollView != null) && (donateGrid != null) && _scrollNotYetDone)
            {
                await scrollView.ScrollToAsync(donateGrid, ScrollToPosition.End, false);
                _scrollNotYetDone = false;
            }
        }
Comment 1 John Hardman 2016-09-30 14:25:50 UTC
The leak occurs with ScrollToPosition.End, but seems not to occur with ScrollToPosition.Start
Comment 2 John Hardman 2016-10-12 18:49:08 UTC
Just noticed whilst working on new functionality that ScrollToAsync can fail to return from the await. I wonder if this may be related.

With an Entry and a Button in a ScrollView on a page, where completing the Entry should result in the Button scrolling into view (if it is not already visible) and the focus being put on the Button (this is Windows so this is a reasonable thing to do), the following code should be ok. However, the await never returns on UWP using 2.3.1.114

        private async void _userPasswordEntry_Completed(object sender, System.EventArgs e)
        {
            _userPasswordEntry?.Unfocus();
            if (_signInButton != null)
            {
                if (_svPage != null)
                    await _svPage.ScrollToAsync(_signInButton, ScrollToPosition.MakeVisible, false); // UWP never returns from ScrollToAsync
                _signInButton.Focus();
            }
        }
Comment 3 John Hardman 2016-10-19 19:23:09 UTC
Yes, it seems that the underlying reason for the memory leak is that the await of ScrollToAsync never returns on Windows platforms. It does on Android and iOS. Let me know if you want me to close this bug and open a new one.
Comment 4 Rui Marinho 2017-01-04 17:11:35 UTC
Thank you for taking the time to submit the bug. We tried to reproduce the issue you reported but were unable given the description. If you could please attach a reproduction to the bug by starting with a clean Xamarin.Forms project and adding just the code necessary to demonstrate the issue, we would very much appreciate it.  


Warm Regards

Xamarin Forms Team
Comment 5 John Hardman 2017-01-05 10:11:47 UTC
@Rui Marinho - this demonstrates the problem on XF 2.3.3.175 . Run this on UWP and follow the on-screen instruction.

using System.Threading.Tasks;

using Xamarin.Forms;

namespace Bug44940
{
    public class App : Application
    {
        private Label _statusLabel;
        private Entry _firstEntry;
        private Entry _secondEntry;
        private StackLayout _verticalStackLayout;
        private ScrollView _scrollView;

        public App()
        {
            _statusLabel = new Label
            {
                Text = "With focus on first Entry, hit Return key",
                HorizontalOptions = LayoutOptions.CenterAndExpand,
                LineBreakMode = LineBreakMode.WordWrap
            };

            _firstEntry = new Entry
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.CenterAndExpand,
            };

            _secondEntry = new Entry
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.CenterAndExpand,
            };

            _firstEntry.Completed += FirstEntryCompleted;

            _verticalStackLayout = new StackLayout
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand,
                Padding = new Thickness(0, 0, 0, 0),
                Margin = new Thickness(0, 0, 0, 0),
                Spacing = 5,
                Children =
                {
                    _statusLabel,
                    _firstEntry,
                    _secondEntry
                }
            };

            _scrollView = new ScrollView
            {
                Orientation = ScrollOrientation.Vertical,
                HorizontalOptions = LayoutOptions.CenterAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand,
                Margin = new Thickness(10, 5, 10, 0),
                Padding = new Thickness(0, 0, 0, 0),
                Content = _verticalStackLayout
            };

            // The root page of your application
            ContentPage contentPage = new ContentPage
            {
                Title = "Bug44940",
                Padding = new Thickness(5, 0, 5, 0),
                Content = _scrollView // vslOuterPage
            };

            MainPage = new NavigationPage(contentPage);

            // not how I do this in reality :-)
            Device.BeginInvokeOnMainThread(async () =>
            {
                await Task.Delay(100);
                _firstEntry.Focus();
            });
        }

        private async void FirstEntryCompleted(object sender, System.EventArgs e)
        {

            _firstEntry?.Unfocus();
            _statusLabel.Text = "Attempting scroll. Return from await pending...";
            await _scrollView.ScrollToAsync(_secondEntry, ScrollToPosition.MakeVisible, false); // UWP never returns from ScrollToAsync
            _statusLabel.Text = "Never get here :-(";
            _secondEntry?.Focus();
        }

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }
}
Comment 6 Rui Marinho 2017-01-05 10:21:47 UTC
Thanks John!
Comment 7 Rui Marinho 2017-02-17 10:49:13 UTC
Should be fixed in 2.3.5-pre1

Note You need to log in before you can comment on or make changes to this bug.