Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
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
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.
For some reason we crash when we pass a zero jni handle to native.
Is this a crash in dalvik or our code? If it's in our code, we better fix it.
If it's in dalvik, we better offer to handle it under some configuration/option as the native crash makes us look bad
and there are cases where user code bug can trigger this.
It doesn't help if we crash if an user call methods on disposed objects.
> Is this a crash in dalvik or our code?
> If it's in dalvik, we better offer to handle it under some configuration/option
> as the native crash makes us look bad
Do you have any suggestions on what we should do?
Some cursory suggestions:
1. Make Java.Lang.Object.Handle throw if it's IntPtr.Zero
2. JNIEnv.Call*Method() can check for a null `this` pointer, then
2.a) Generate a warning, then ignore the call
2.b) Generate a warning and abort the process
> It doesn't help if we crash if an user call methods on disposed objects.
I'm less caring if someone explicitly disposes of an object and then things blow up:
var o = new Java.Lang.Object();
string s = o.ToString(); // BOOM
However, if/when the GC collects the instance (GC bugs! See Bug #13858 and Bug #14999), things get more confusing.
To return to the "cursory suggestions":
The problem with (1) is that, while it sounds fairly clean (calling a method on an invalidated object would crash the process with an unhandled exception, with a plausibly useful message), it has a flaw:
It provides no way for the user to manually workaround GC bugs unless they use try/catch.
(I suppose this could be opt-in via AndroidEnvironment...)
(2a) just sounds evil.
(2b) should be reasonably easy to do.
monodroid/c71b6125 implements (3): JNIEnv.* methods null-check their arguments and will throw an ArgumentException or ArgumentNullException if IntPtr.Zero or null is passed when it is inappropriate.
Consequently, `o.ToString()` now results in an ArgumentException instead of a process abort.
Keeping this open in case we need to discuss alternatives.