diff --git a/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs b/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs index 80d46e322..49a5c1ab0 100644 --- a/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs +++ b/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs @@ -44,9 +44,13 @@ partial void SetValueManager (CreationOptions options) public abstract partial class JniValueManager : ISetRuntime, IDisposable { public JniRuntime Runtime { get; private set; } + protected bool disposed = false; public virtual void OnSetRuntime (JniRuntime runtime) { + if (disposed) + throw new ObjectDisposedException (GetType ().Name); + Runtime = runtime; } @@ -58,6 +62,7 @@ public void Dispose () protected virtual void Dispose (bool disposing) { + disposed = true; } public abstract void WaitForGCBridgeProcessing (); @@ -123,6 +128,9 @@ public int GetJniIdentityHashCode (JniObjectReference reference) public virtual void DisposePeer (IJavaPeerable value) { + if (disposed) + throw new ObjectDisposedException (GetType ().Name); + if (value == null) throw new ArgumentNullException (nameof (value)); @@ -135,6 +143,9 @@ public virtual void DisposePeer (IJavaPeerable value) void DisposePeer (JniObjectReference h, IJavaPeerable value) { + if (disposed) + throw new ObjectDisposedException (GetType ().Name); + value.Disposed (); RemovePeer (value); var o = Runtime.ObjectReferenceManager; @@ -162,6 +173,9 @@ void DisposePeer (JniObjectReference h, IJavaPeerable value) public virtual void DisposePeerUnlessReferenced (IJavaPeerable value) { + if (disposed) + throw new ObjectDisposedException (GetType ().Name); + if (value == null) throw new ArgumentNullException (nameof (value)); @@ -180,6 +194,9 @@ public virtual void DisposePeerUnlessReferenced (IJavaPeerable value) public object PeekValue (JniObjectReference reference) { + if (disposed) + throw new ObjectDisposedException (GetType ().Name); + if (!reference.IsValid) return null; @@ -237,6 +254,9 @@ static Type GetPeerType (Type type) public virtual IJavaPeerable CreatePeer (ref JniObjectReference reference, JniObjectReferenceOptions transfer, Type targetType) { + if (disposed) + throw new ObjectDisposedException (GetType ().Name); + targetType = targetType ?? typeof (JavaObject); targetType = GetPeerType (targetType); @@ -311,6 +331,9 @@ static ConstructorInfo GetActivationConstructor (Type type) public object CreateValue (ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType = null) { + if (disposed) + throw new ObjectDisposedException (GetType ().Name); + if (!reference.IsValid) return null; @@ -337,6 +360,9 @@ public object CreateValue (ref JniObjectReference reference, JniObjectReferenceO public T CreateValue (ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType = null) { + if (disposed) + throw new ObjectDisposedException (GetType ().Name); + if (!reference.IsValid) return default (T); @@ -374,6 +400,9 @@ internal Type GetRuntimeType (JniObjectReference reference) public object GetValue (ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType = null) { + if (disposed) + throw new ObjectDisposedException (GetType ().Name); + if (!reference.IsValid) return null; @@ -435,6 +464,9 @@ public T GetValue (ref JniObjectReference reference, JniObjectReferenceOption public JniValueMarshaler GetValueMarshaler() { + if (disposed) + throw new ObjectDisposedException (GetType ().Name); + var m = GetValueMarshaler (typeof (T)); var r = m as JniValueMarshaler; if (r != null) @@ -449,6 +481,9 @@ public JniValueMarshaler GetValueMarshaler() public JniValueMarshaler GetValueMarshaler (Type type) { + if (disposed) + throw new ObjectDisposedException (GetType ().Name); + if (type == null) throw new ArgumentNullException ("type"); var info = type.GetTypeInfo (); diff --git a/src/Java.Runtime.Environment/Java.Interop/MonoRuntimeValueManager.cs b/src/Java.Runtime.Environment/Java.Interop/MonoRuntimeValueManager.cs index 66ab25ded..66c38c616 100644 --- a/src/Java.Runtime.Environment/Java.Interop/MonoRuntimeValueManager.cs +++ b/src/Java.Runtime.Environment/Java.Interop/MonoRuntimeValueManager.cs @@ -66,6 +66,11 @@ public override void CollectPeers () protected override void Dispose (bool disposing) { + if (disposed) + return; + + base.Dispose (disposing); + if (!disposing) return;