Skip to content
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

[Java.Interop] Dispose, *then* Remove #708

Merged
merged 1 commit into from
Sep 5, 2020
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
3 changes: 1 addition & 2 deletions src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,8 @@ public virtual void DisposePeer (IJavaPeerable value)
if (!value.PeerReference.IsValid)
return;

RemovePeer (value);

value.Disposed ();
RemovePeer (value);

var h = value.PeerReference;
if (!h.IsValid)
Expand Down
1 change: 1 addition & 0 deletions tests/Java.Interop-Tests/Java.Interop-Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<JavaInteropTestJar Include="$(MSBuildThisFileDirectory)java\com\xamarin\interop\CallNonvirtualDerived2.java" />
<JavaInteropTestJar Include="$(MSBuildThisFileDirectory)java\com\xamarin\interop\CallVirtualFromConstructorBase.java" />
<JavaInteropTestJar Include="$(MSBuildThisFileDirectory)java\com\xamarin\interop\CallVirtualFromConstructorDerived.java" />
<JavaInteropTestJar Include="$(MSBuildThisFileDirectory)java\com\xamarin\interop\GetThis.java" />
<JavaInteropTestJar Include="$(MSBuildThisFileDirectory)java\com\xamarin\interop\SelfRegistration.java" />
<JavaInteropTestJar Include="$(MSBuildThisFileDirectory)java\com\xamarin\interop\TestType.java" />
</ItemGroup>
Expand Down
47 changes: 47 additions & 0 deletions tests/Java.Interop-Tests/Java.Interop/GetThis.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;

using Java.Interop;

namespace Java.InteropTests
{
[JniTypeSignature (GetThis.JniTypeName)]
public class GetThis : JavaObject
{
internal const string JniTypeName = "com/xamarin/interop/GetThis";

bool _isDisposed;

readonly static JniPeerMembers _members = new JniPeerMembers (JniTypeName, typeof (GetThis));

public override JniPeerMembers JniPeerMembers {
get {return _members;}
}

public GetThis ()
{
}

public unsafe GetThis This {
get {
var o = _members.InstanceMethods.InvokeNonvirtualObjectMethod ("getThis.()Lcom/xamarin/interop/GetThis;", this, null);
return JniEnvironment.Runtime.ValueManager.GetValue<GetThis> (ref o, JniObjectReferenceOptions.CopyAndDispose);
}
}

protected override void Dispose (bool disposing)
{
if (_isDisposed) {
return;
}
_isDisposed = true;
if (disposing) {
var t = This;
if (t != this) {
throw new InvalidOperationException ("SHOULD NOT BE REACHED");
}
}
base.Dispose (disposing);
}
}
}

8 changes: 8 additions & 0 deletions tests/Java.Interop-Tests/Java.Interop/JavaObjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ public void NestedDisposeInvocations ()
value.Dispose ();
value.Dispose ();
}

[Test]
public void DisposeAccessesThis ()
{
var value = new GetThis ();
value.Dispose ();
value.Dispose ();
}
}

class JavaObjectWithNoJavaPeer : JavaObject {
Expand Down
42 changes: 42 additions & 0 deletions tests/Java.Interop-Tests/java/com/xamarin/interop/GetThis.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.xamarin.interop;

import java.util.ArrayList;

import com.xamarin.java_interop.GCUserPeerable;

public class GetThis implements GCUserPeerable {

static final String assemblyQualifiedName = "Java.InteropTests.GetThis, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null";
static {
com.xamarin.java_interop.ManagedPeer.registerNativeMembers (
GetThis.class,
assemblyQualifiedName,
"");
}

ArrayList<Object> managedReferences = new ArrayList<Object>();

public GetThis () {
if (GetThis.class == getClass ()) {
com.xamarin.java_interop.ManagedPeer.construct (
this,
"Java.InteropTests.GetThis, Java.Interop-Tests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
""
);
}
}

public final GetThis getThis() {
return this;
}

public void jiAddManagedReference (java.lang.Object obj)
{
managedReferences.add (obj);
}

public void jiClearManagedReferences ()
{
managedReferences.clear ();
}
}