[iOS] Requesting some guidance/clarification on potential Memory Leaks

Hello,

I'm currently doing some investigations on a codebase I've inherited, and I'm looking at potential memory leaks. I just wanted to get some guidance on what I'm seeing, as I'm pretty sure this is a potential memory leak, but it seems obvious to the point of it being a little too obvious. Also, because its currently hosted within a custom control (that doesn't have its own renderer as far as I can see), I wanted to see if the resource cleanup behaviours are different in that context.

So, I've got a custom control that is made up of a XAML file, and a c# codebehind file. This control contains a Grid in the XAML file, with some logic wiring things up behind the scenes. This also overrides LayoutChildren, and adds a Scrolled handler to the parent ScrollView.

    public partial class CustomPicker
    {

        public CustomPicker()
        {
            InitializeComponent();

            var tapGestureRecognizer = new TapGestureRecognizer();
            tapGestureRecognizer.Tapped += (sender, args) => DisplayPicker();

            GestureRecognizers.Add(tapGestureRecognizer);
        }

       protected override void LayoutChildren(double x, double y, double width, double height)
        {
            base.LayoutChildren(x, y, width, height);

            var parentView = Parent as VisualElement;

            while (!(parentView is ExtendedPage))
            {
                var scrollView = parentView as ScrollView;

                if (scrollView != null)
                {
                    scrollView.Scrolled += (sender, e) =>
                    {
                        _scrollY = scrollView.ScrollY;

                        if (PickerText.IsFocused) PickerText.Unfocus();
                    };
                }

                parentView = parentView.Parent as VisualElement;
            }
        }
    }

As you can see, this is doing some other stuff, but the two main points of my concern are how its wiring up that Scrolled event in LayoutChildren, and how its wiring up the Tapped event in the constructor.

According to what I've seen in the Xamarin Forms docs (specifically the Cross-Platform Performance guide, I'd normally provide a link but apparently as this is a new account I can't post links at the moment), would I be correct in saying this looks like two potential memory leaks? This is because I can't see elsewhere in the control where it's unsubscribing from the Scrolled event, nor clearing out the GestureRecognizers attached to the control.

Silly question(s) time: Are custom controls automatically somehow cleaned up, due to the fact the View part of this control is wired up through XAML, and Xamarin somehow cleans it up correctly? That doesn't sound right to be, but I thought it was worth checking before attempting to "fix" it.

Does the way the Tapped gesture recognizer being wired up in this fashion mean its also holding onto that behind the scenes too?

Can I change this class to inherit from IDisposable, and manually unsubscribe from these events in that fashion? Would I be correct in stating I'd still need to explicitly call Dispose() from somewhere, as (to my knowledge) XF doesn't magically wrap everything in a using statement?

Any help would be greatly appreciated :)

Cheers

Answers

Sign In or Register to comment.