Bug 6715 - System.WeakReference problem
Summary: System.WeakReference problem
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 4.2.x
Hardware: All All
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2012-08-27 16:50 UTC by Alexey Subbotin
Modified: 2012-10-03 14:32 UTC (History)
3 users (show)

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 Alexey Subbotin 2012-08-27 16:50:07 UTC
System.WeakReference.Target not always correctly cleared during gc. In the sample below the second garbage collection leaves object referenced by Target property in incorrect state (the managed peer is alive while a corresponding native peer in Java has already been removed). The provided sample throws NRE deep inside ToString call:

	static WeakReference CreateBitmap()
		return new WeakReference(Bitmap.CreateBitmap(10, 10, Bitmap.Config.Argb8888));

	static void Test()
		var bitmap = CreateBitmap();


		Console.WriteLine(bitmap.Target); // NRE inside ToString(), after successfull dereferencing


Exception info:
System.NullReferenceException: Object reference not set to an instance of an object
  at Android.Runtime.JNIEnv.CallObjectMethod (intptr,intptr) [0x00000] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:129

  at Java.Lang.Object.ToString () [0x0003e] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Java.Lang.Object.cs:189

  at System.IO.TextWriter.Write (object) [0x00006] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/mono/mcs/class/corlib/System.IO/TextWriter.cs:163

  at System.IO.TextWriter.WriteLine (object) [0x00000] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/mono/mcs/class/corlib/System.IO/TextWriter.cs:273

  at System.IO.SynchronizedWriter.WriteLine (object) [0x0000c] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/mono/mcs/class/corlib/System.IO/TextWriter.cs:563

  at System.Console.WriteLine (object) [0x00000] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/mono/mcs/class/corlib/System/Console.cs:432

  at TestWeaksAndroid.Activity1.Test () [0x0001f] in d:\Projects\TestWeaksAndroid\TestWeaksAndroid\Activity1.cs:38

  at TestWeaksAndroid.Activity1.OnCreate (Android.OS.Bundle) [0x00016] in d:\Projects\TestWeaksAndroid\TestWeaksAndroid\Activity1.cs:48

  at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00010] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/9f7cbd60/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Android.App.Activity.cs:1490

  at (wrapper dynamic-method) object.6388eb25-5ff0-4675-9fed-ebae070e8844 (intptr,intptr,intptr) <IL 0x00012, 0x00033>
Comment 1 Alexey Subbotin 2012-08-27 17:02:10 UTC
WeakReference.Target property becomes null only after third garbage collection.
Comment 2 Mark Probst 2012-09-17 17:16:44 UTC
A fix is on the way.

As it is right now, weak references to objects which have Java peers will stay around for one more garbage collection, during which time their Java peers will be gone, i.e. they will act as if they have been disposed.

If you need the correct behaviour right now, you could let the WeakReference reference the object through an intermediary managed-only object.
Comment 3 Mark Probst 2012-10-03 14:32:53 UTC