Bug 43093 - Long WeakReference.Target for Renderer nulled after handler Finalize is called and before Renderer finalizer is called
Summary: Long WeakReference.Target for Renderer nulled after handler Finalize is calle...
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Rui Marinho
URL:
Depends on:
Blocks:
 
Reported: 2016-08-05 15:54 UTC by philip
Modified: 2017-06-19 21:30 UTC (History)
4 users (show)

Tags:
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:
Status:
RESOLVED FIXED

Description philip 2016-08-05 15:54:04 UTC
The snipped below shows the problem.  I only tested this under Android. Basically the a long WeakReference "theReference" to the ImageCircleRenderer should have a valid target when the ImageCircleRenderer's finalizer is called.  However, the theReference.Target is nulled when the CircleImage finalizer is called.  It appears that XF/mono is breaking the weakreference.  This happens ONLY with custom render objects, so this is something specific to custom renderers.


[assembly: Xamarin.Forms.ExportRenderer(typeof(TDSP.Controls.CircleImage), typeof(TDSP.Controls.Droid.ImageCircleRenderer))]
namespace XYZ
{
    public class ImageCircleRenderer : ImageRenderer
    {
	~ImageCircleRenderer()
        {
          // When we get here, theReference.Target is null, but it should not be since this is a long
          // WeakReference and this object finalizer did not finish executing.
	}
    }
}

namespace XYZ
{
    public class CircleImage : Image
    {
        public CircleImage ()
        {
	}

	~CircleImage ()
        {
	}
    }
}

main code:

WeakReference theReference = WeakReference(ImageCircleRenderer_instance, true);
Comment 1 Rui Marinho 2016-08-09 14:56:58 UTC
Can you override Dispose and don't call base to see if fixes the issue?
Comment 2 philip 2016-08-09 15:57:53 UTC
adding this to the ImageCircleRenderer does not make any difference:

		protected override void Dispose(bool disposing)
		{
		}

I also found that ~ImageCircleRenderer() is not called at all for 33 out of 44 instances, yet the Xamarin Profiler shows that no instances are leaked.  So it seems that the Finalizer is skipped altogether in many cases, which definitely look like a XF bug.  I am guessing finalization is suppressed somewhere?
Comment 3 Rui Marinho 2017-06-19 21:30:16 UTC
Thank you for taking the time to submit this report. After reviewing the description of this bug, we believe it no longer affects the current version of Xamarin.Forms. If you are still experiencing the issue after updating your packages, please reopen this report with an attached reproduction.

Here are some reproduction best practices: https://gist.github.com/jassmith/92405c300e54a01dcc6d