Skip to content

Commit

Permalink
[Java.Interop] Check for disposed state in JniValueManager methods
Browse files Browse the repository at this point in the history
  • Loading branch information
radekdoulik committed Jan 3, 2018
1 parent 19379a3 commit d942d9a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -58,6 +62,7 @@ public void Dispose ()

protected virtual void Dispose (bool disposing)
{
disposed = true;
}

public abstract void WaitForGCBridgeProcessing ();
Expand Down Expand Up @@ -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));

Expand All @@ -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;
Expand Down Expand Up @@ -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));

Expand All @@ -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;

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand All @@ -337,6 +360,9 @@ public object CreateValue (ref JniObjectReference reference, JniObjectReferenceO

public T CreateValue<T> (ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType = null)
{
if (disposed)
throw new ObjectDisposedException (GetType ().Name);

if (!reference.IsValid)
return default (T);

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -435,6 +464,9 @@ public T GetValue<T> (ref JniObjectReference reference, JniObjectReferenceOption

public JniValueMarshaler<T> GetValueMarshaler<T>()
{
if (disposed)
throw new ObjectDisposedException (GetType ().Name);

var m = GetValueMarshaler (typeof (T));
var r = m as JniValueMarshaler<T>;
if (r != null)
Expand All @@ -449,6 +481,9 @@ public JniValueMarshaler<T> GetValueMarshaler<T>()

public JniValueMarshaler GetValueMarshaler (Type type)
{
if (disposed)
throw new ObjectDisposedException (GetType ().Name);

if (type == null)
throw new ArgumentNullException ("type");
var info = type.GetTypeInfo ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public override void CollectPeers ()

protected override void Dispose (bool disposing)
{
if (disposed)
return;

base.Dispose (disposing);

if (!disposing)
return;

Expand Down

0 comments on commit d942d9a

Please sign in to comment.