Bug 16343 - WeakReference to Java object has non-null Target that refers to Java object with zero Handle
Summary: WeakReference to Java object has non-null Target that refers to Java object w...
Status: CONFIRMED
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler (show other bugs)
Version: 4.10.1
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2013-11-19 12:54 UTC by Adam Kemp
Modified: 2013-11-21 04:39 UTC (History)
6 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Test case (16.82 KB, application/x-download)
2013-11-19 12:54 UTC, Adam Kemp
Details


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 for Bug 16343 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description Adam Kemp 2013-11-19 12:54:26 UTC
I discovered that in some cases our WeakReferences appear to be still alive (Target is non-null), but they refer to objects that have been collected by the Java GC (i.e., their Handle is IntPtr.Zero). I don't think this is correct behavior. If a C# WeakReference refers to a C# object that has a Java peer then collecting the Java peer should invalidate the WeakReference.

To reproduce the issue build and run the attached project. Each time you press the button we start adding 10 new objects (kept track of via WeakReferences) to a list 10 times a second. Pressing the button again essentially starts new threads, causing objects to be created and added more quickly. We also go through the list each time to check for stale references to remove them from the list.

When a WeakReference is encountered with a non-null Target that refers to an IJavaObject with a zero Handle then an exception is thrown. When I run this test I get an exception somewhere around 1600 objects.
Comment 1 Adam Kemp 2013-11-19 12:54:50 UTC
Created attachment 5474 [details]
Test case
Comment 2 Rodrigo Kumpera 2013-11-19 13:11:25 UTC
This is a known issue that we're aware of, have have add a workaround in previous version (more than 6 months ago) that reduces the likelihood of witnessing this.

The workaround is to check if the returned object is ok or not. Which can be done by subclassing WeakReference. Does that work for you?

We don't have any short term plans on fixing this one.
Comment 3 Adam Kemp 2013-11-19 14:25:07 UTC
We can add a workaround in the one case in which we know for sure that we are hitting this, but we have many other places in our large code base where we use a WeakReference that may possibly refer to a Java object. Nearly all of that code is cross-platform code, and it is not practical to modify every one of those places to use a wrapper or to have a special case for Android.