Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Java.Interop] Improve finalization support.
Finalizers may be run in any order, and if the process is being torn down and/or the JavaVM/JniEnvironment has been disposed, then it's not really valid to try to use anything that depends on them. Unfortunately, many of the SafeHandle types DO depend on them, via handle tracking; see commit d0b8f1d. This can result in an exception during process teardown: Unhandled Exception: System.ObjectDisposedException: The object was used after being disposed. at System.Threading.ThreadLocal`1[Java.Interop.JniEnvironment].get_Value () [0x00000] in <filename unknown>:0 at Java.Interop.JniEnvironment.get_RootEnvironment () [0x00006] in /Users/jon/Dropbox/Developer/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.cs:133 at Java.Interop.JniEnvironment.get_Current () [0x0000d] in /Users/jon/Dropbox/Developer/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.cs:123 at Java.Interop.JniMethodID.ReleaseHandle () [0x00001] in /Users/jon/Dropbox/Developer/Java.Interop/src/Java.Interop/Java.Interop/JniMethodID.cs:16 at System.Runtime.InteropServices.SafeHandle.Finalize () [0x00016] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-r JniEnvironment.Current is no more, the JavaVM was Dispose()d, and JniEnvironment.RootEnvironments was Dispose()d. To avoid this, add a JniEnvironment.HasCurrent property, which checks to see if JniEnvironment.Current would be valid (-ish; it just checks JniEnvironment.current, not JniEnvironment.RootEnvironments), and modify the *SafeHandle.ReleaseHandle() methods to only call JavaVM.UnTrack() if JniEnvironment.HasCurrent is true. Possible bug/something to ponder: should JavaVM actually dispose of JniEnvironment.RootEnvironments? 99.9999% of the time, that's the right thing to do, but if/when there's ever a JVM which allows having more than one JavaVM within the process, ever, then this could be problematic...
- Loading branch information