Skip to content

Commit 2721667

Browse files
committed
[Java.Interop] Check for disposed state in JniValueManager methods
Catched by Gendarme's https://github.com/spouliot/gendarme/wiki/Gendarme.Rules.Exceptions.UseObjectDisposedExceptionRule(2.10)
1 parent 19379a3 commit 2721667

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs

+35
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ partial void SetValueManager (CreationOptions options)
4444
public abstract partial class JniValueManager : ISetRuntime, IDisposable {
4545

4646
public JniRuntime Runtime { get; private set; }
47+
protected bool disposed = false;
4748

4849
public virtual void OnSetRuntime (JniRuntime runtime)
4950
{
51+
if (disposed)
52+
throw new ObjectDisposedException (GetType ().Name);
53+
5054
Runtime = runtime;
5155
}
5256

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

5963
protected virtual void Dispose (bool disposing)
6064
{
65+
disposed = true;
6166
}
6267

6368
public abstract void WaitForGCBridgeProcessing ();
@@ -123,6 +128,9 @@ public int GetJniIdentityHashCode (JniObjectReference reference)
123128

124129
public virtual void DisposePeer (IJavaPeerable value)
125130
{
131+
if (disposed)
132+
throw new ObjectDisposedException (GetType ().Name);
133+
126134
if (value == null)
127135
throw new ArgumentNullException (nameof (value));
128136

@@ -135,6 +143,9 @@ public virtual void DisposePeer (IJavaPeerable value)
135143

136144
void DisposePeer (JniObjectReference h, IJavaPeerable value)
137145
{
146+
if (disposed)
147+
throw new ObjectDisposedException (GetType ().Name);
148+
138149
value.Disposed ();
139150
RemovePeer (value);
140151
var o = Runtime.ObjectReferenceManager;
@@ -162,6 +173,9 @@ void DisposePeer (JniObjectReference h, IJavaPeerable value)
162173

163174
public virtual void DisposePeerUnlessReferenced (IJavaPeerable value)
164175
{
176+
if (disposed)
177+
throw new ObjectDisposedException (GetType ().Name);
178+
165179
if (value == null)
166180
throw new ArgumentNullException (nameof (value));
167181

@@ -180,6 +194,9 @@ public virtual void DisposePeerUnlessReferenced (IJavaPeerable value)
180194

181195
public object PeekValue (JniObjectReference reference)
182196
{
197+
if (disposed)
198+
throw new ObjectDisposedException (GetType ().Name);
199+
183200
if (!reference.IsValid)
184201
return null;
185202

@@ -237,6 +254,9 @@ static Type GetPeerType (Type type)
237254

238255
public virtual IJavaPeerable CreatePeer (ref JniObjectReference reference, JniObjectReferenceOptions transfer, Type targetType)
239256
{
257+
if (disposed)
258+
throw new ObjectDisposedException (GetType ().Name);
259+
240260
targetType = targetType ?? typeof (JavaObject);
241261
targetType = GetPeerType (targetType);
242262

@@ -311,6 +331,9 @@ static ConstructorInfo GetActivationConstructor (Type type)
311331

312332
public object CreateValue (ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType = null)
313333
{
334+
if (disposed)
335+
throw new ObjectDisposedException (GetType ().Name);
336+
314337
if (!reference.IsValid)
315338
return null;
316339

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

338361
public T CreateValue<T> (ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType = null)
339362
{
363+
if (disposed)
364+
throw new ObjectDisposedException (GetType ().Name);
365+
340366
if (!reference.IsValid)
341367
return default (T);
342368

@@ -374,6 +400,9 @@ internal Type GetRuntimeType (JniObjectReference reference)
374400

375401
public object GetValue (ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType = null)
376402
{
403+
if (disposed)
404+
throw new ObjectDisposedException (GetType ().Name);
405+
377406
if (!reference.IsValid)
378407
return null;
379408

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

436465
public JniValueMarshaler<T> GetValueMarshaler<T>()
437466
{
467+
if (disposed)
468+
throw new ObjectDisposedException (GetType ().Name);
469+
438470
var m = GetValueMarshaler (typeof (T));
439471
var r = m as JniValueMarshaler<T>;
440472
if (r != null)
@@ -449,6 +481,9 @@ public JniValueMarshaler<T> GetValueMarshaler<T>()
449481

450482
public JniValueMarshaler GetValueMarshaler (Type type)
451483
{
484+
if (disposed)
485+
throw new ObjectDisposedException (GetType ().Name);
486+
452487
if (type == null)
453488
throw new ArgumentNullException ("type");
454489
var info = type.GetTypeInfo ();

src/Java.Runtime.Environment/Java.Interop/MonoRuntimeValueManager.cs

+5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public override void CollectPeers ()
6666

6767
protected override void Dispose (bool disposing)
6868
{
69+
if (disposed)
70+
return;
71+
72+
base.Dispose (disposing);
73+
6974
if (!disposing)
7075
return;
7176

0 commit comments

Comments
 (0)