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)

See Also:
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

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.

Note You need to log in before you can comment on or make changes to this bug.