Bug 27457 - Memory leak on Android with Custom Control/Renderer
Summary: Memory leak on Android with Custom Control/Renderer
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.3.4
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2015-02-26 09:38 UTC by ron.jacobs@newellco.com
Modified: 2016-04-08 19:32 UTC (History)
7 users (show)

Tags: android customrenderer memory
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 ron.jacobs@newellco.com 2015-02-26 09:38:45 UTC
I have a sample application that shows the issue:

To reproduce:
1. Run the sample app on Android
2. Click the "Show The Circle Form" button
3. Click the "Grow Circle" button
4. Navigate back one page.
5. Repeat steps 2 and 3
6. Crash

The crash is caused by a memory leaked instance of the control attempting to redraw after it has been "partially" disposed.
It there was a way for the control to know that it was being disposed/destroyed, I could unsubscribe to the events that cause the crash.  On iOS the custom renderers OnElementChanged method is called with a newValue of null and an oldValue that is non Null indicating that I can unsubscribe my events.  If we could make that happen on Android, that would be great or any consistent call to the renderer or the control would work.
Comment 1 asimk 2015-02-26 11:59:08 UTC
I have check this issue with the help of sample app attached in Bug description and able to reproduce it. I observed that when I click "Show The Circle Form" button, click  "Grow Circle" button, navigate back and again repeat same process then getting exception "System.ArgumentException: 'jobject' must not be IntPtr.Zero.". 

Screencast: http://www.screencast.com/t/mGtGVvMR7
Debug Output: https://gist.github.com/saurabh360/67062b8de3895e77d419
Xamarin log: https://gist.github.com/saurabh360/68f8e9a9597fd28ace6d

Environment Info : 
Microsoft Visual Studio Professional 2013
Version 12.0.31101.00 Update 4
Microsoft .NET Framework
Version 4.5.51641

Xamarin   3.9.346.0 (d6de844)
Xamarin.Android (49a04b966feb40dfdba49d57ba16249b66d606a6)
Xamarin.iOS (862702c6b8aa14a373e25f83ac45918556af41a2)
Xamarin.iOS Unified Migration   1.0
Comment 2 Chris King 2015-03-04 17:07:24 UTC
Circle radius changes should fire an INotifyPropertyChange that the customer renderer sees and then applies to the platform view. I see no logic in the renderer to test for the interception of radius property changing. I see no logic in the renderer to test for the interception of radius property changing. Instead, an event is attached to OnRenderChange that directly manipulates the view. Review the documentation for creating a renderer.
Comment 3 ron.jacobs@newellco.com 2015-03-05 09:01:36 UTC
That may be so, but that is not the issue here.  There needs to be a way for a custom control to clean up its resources if it is being destroyed.  Currently there is a way in iOS, but not for Android.  With this very simple example, what you propose would eliminate the event handling in the control, but my real controls are quite a bit more complicated.
Comment 4 Chris King 2015-06-01 17:13:18 UTC
Internally we detach events in an override of dispose. We are aware of the asymmetry of OnRendererChange between iOS and Android and would like to unify them except, unfortunately,  it would be a breaking change. If the dispose approach doesn't work for you please feel free to submit a reduced reproduction of your use case and we'll be happy look at it. 

Warm regards,
Xamarin Forms Team
Comment 5 E.Z. Hart [MSFT] 2016-04-08 19:32:29 UTC
Thank you for your bug report. As we have not received the information requested, we cannot properly triage your bug and are marking it resolved.

Warm regards,
Xamarin Forms Team