Skip to content

[Java.Interop] Check for disposed state in JniValueManager methods #239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; }
bool disposed;

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,8 @@ public override void CollectPeers ()

protected override void Dispose (bool disposing)
{
base.Dispose (disposing);

if (!disposing)
return;

Expand All @@ -82,6 +84,7 @@ protected override void Dispose (bool disposing)
}
}
RegisteredInstances.Clear ();
RegisteredInstances = null;
}
}

Expand Down