1 year ago

#73868

test-img

T.Eswaran

How to resolve the memory leak issue, when a custom control is placed into the Collection view data template

I designed a custom control that will contain content and will be primarily used to display a basic animation before loading the content. We used ValueAnimator to start and stop the animation in our custom control. When we placed the custom control inside the Collection view data template, we encountered a memory leak on the Xamarin forms Android platform. We used a garbage collector and added a destructor method to the content page to check for memory leaks. When we investigated the problem, we discovered that it only occurs when the custom control is placed within the collection view. We used the update event in the value animator to refresh the view, as shown in the code below.

`public void AddWaveAnimator()
        {
            if (animator == null)
            {
                animator = ValueAnimator.OfFloat(0, 1);
                animator.RepeatCount = ValueAnimator.Infinite;
                animator.SetInterpolator(new Views.Animations.LinearInterpolator());
                animator.Update += OnWaveAnimatorUpdate;
            }
            else
            {
                animator.End();
            }
            animator.SetDuration((int)(1000d));
            animator.Start();
        }
private void OnWaveAnimatorUpdate(object sender, ValueAnimator.AnimatorUpdateEventArgs e)
        {
            Invalidate();
        }
`

Surprisingly, the issue does not occur when the animator update event is commented out. When invoking the update event, the memory leak happens. I'm not sure if it's a framework issue or not because the problem only happens when the custom control is placed inside the collection view. Is there anyone who knows how to address this problem? Please get the sample from the below link. Click here to download Sample

Steps to Reproduce

  1. In the custom control page, we have a destructor method to check whether the memory is correctly disposed or not.
  2. Keep a break point in the destructor method and run the sample.
  3. When navigating back and forth to the custom control page the destructor method will not be raised
  4. If we remove the below line animator.Update += OnWaveAnimatorUpdate;

The destructor method is called correctly.

Expected Behavior

The destructor method should be called when navigating back and forth to the custom control page

Actual Behavior

When navigating back and forth to the custom control page the destructor method will not be raised

xamarin.forms

memory-leaks

xamarin.android

custom-controls

collectionview

0 Answers

Your Answer

Accepted video resources