Skip to content

Commit 9da85b9

Browse files
[Mono.Android] fix "replaceable" objects in ManagedValueManager
The following test is failing on NativeAOT as well as any case we'd use `ManagedValueManager`: [Test] public void JnienvCreateInstance_RegistersMultipleInstances () { using (var adapter = new CreateInstance_OverrideAbsListView_Adapter (Application.Context)) { var intermediate = CreateInstance_OverrideAbsListView_Adapter.Intermediate; var registered = Java.Lang.Object.GetObject<CreateInstance_OverrideAbsListView_Adapter>(adapter.Handle, JniHandleOwnership.DoNotTransfer); Assert.AreNotSame (adapter, intermediate); // Passes Assert.AreSame (adapter, registered); // Fails! } } With the assertion: Expected: same as <com.xamarin.android.runtimetests.CreateInstance_OverrideAbsListView_Adapter{cbd0e5a V.ED.VC.. ......I. 0,0-0,0}> But was: <com.xamarin.android.runtimetests.CreateInstance_OverrideAbsListView_Adapter{cbd0e5a V.ED.VC.. ......I. 0,0-0,0}> The second assertion fails because `registered` is the same instance as `intermediate`. In this example, this is a code path where `intermediate` should be "replaced" with `adapter`. After lots of debugging, I found the problem are these lines in the `ManagedValueManager.AddPeer()` method: var o = PeekPeer (value.PeerReference); if (o != null) return; If we `PeekPeer()` in the middle of `AddPeer()` and a type is "replaceable", it would find an instance and not replace it! I did not find equivalent code in `AndroidValueManager.AddPeer()`, which is what is used in Mono & production today. With these lines removed, the test passes. I will look if we should also update these lines in dotnet/java-interop in a future PR.
1 parent 056589b commit 9da85b9

File tree

1 file changed

+0
-3
lines changed

1 file changed

+0
-3
lines changed

src/Mono.Android/Microsoft.Android.Runtime/ManagedValueManager.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ public override void AddPeer (IJavaPeerable value)
6767
var r = value.PeerReference;
6868
if (!r.IsValid)
6969
throw new ObjectDisposedException (value.GetType ().FullName);
70-
var o = PeekPeer (value.PeerReference);
71-
if (o != null)
72-
return;
7370

7471
if (r.Type != JniObjectReferenceType.Global) {
7572
value.SetPeerReference (r.NewGlobalRef ());

0 commit comments

Comments
 (0)