Skip to content

Commit 0104236

Browse files
[JniValueManager] Make TryConstructPeer virtual (#1456)
Follow-up to #1441 We need this method virtual so that `JavaMarshalValueManager` can override it and try to invoke the `XAConstructorSignature`. The method was previously virtual but as an oversight I removed it in #1441: - https://github.com/dotnet/java-interop/pull/1441/changes#diff-8ead12d157bc7712affd3d920bb93cddbff3885147fe6297957a44cc522d5430R7 - https://github.com/dotnet/java-interop/pull/1441/changes#diff-b92884de5db7f82df63b483b8319360cd9bfdac3cd97222eb9aff7292a01a07bL449-L467 I noticed we're not disposing the `reference` correctly which might cause a ref leak. In the Mono `AndroidValueManager` we never called into the base method so this would not be observed on mono previously (https://github.com/dotnet/android/blob/main/src/Mono.Android/Java.Interop/TypeManager.cs#L416-L423). Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent d7dbad5 commit 0104236

2 files changed

Lines changed: 4 additions & 2 deletions

File tree

src/Java.Interop/Java.Interop/JniRuntime.ReflectionJniValueManager.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ static Type GetPeerType ([DynamicallyAccessedMembers (Constructors)] Type type)
233233
static readonly Type ByRefJniObjectReference = typeof (JniObjectReference).MakeByRefType ();
234234
static readonly Type[] JIConstructorSignature = new Type [] { ByRefJniObjectReference, typeof (JniObjectReferenceOptions) };
235235

236-
bool TryConstructPeer (
236+
protected virtual bool TryConstructPeer (
237237
IJavaPeerable self,
238238
ref JniObjectReference reference,
239239
JniObjectReferenceOptions options,
@@ -247,7 +247,8 @@ bool TryConstructPeer (
247247
options,
248248
};
249249
c.Invoke (self, args);
250-
reference = (JniObjectReference) args [0];
250+
reference = (JniObjectReference) args [0];
251+
JniObjectReference.Dispose (ref reference, options);
251252
return true;
252253
}
253254
return false;

src/Java.Interop/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,4 @@ override Java.Interop.JniRuntime.ReflectionJniTypeManager.GetTypes(Java.Interop.
8585
override Java.Interop.JniRuntime.ReflectionJniTypeManager.GetTypesForSimpleReference(string! jniSimpleReference) -> System.Collections.Generic.IEnumerable<System.Type!>!
8686
override Java.Interop.JniRuntime.ReflectionJniTypeManager.RegisterNativeMembers(Java.Interop.JniType! nativeClass, System.Type! type, string? methods) -> void
8787
override Java.Interop.JniRuntime.ReflectionJniTypeManager.RegisterNativeMembers(Java.Interop.JniType! nativeClass, System.Type! type, System.ReadOnlySpan<char> methods) -> void
88+
virtual Java.Interop.JniRuntime.ReflectionJniValueManager.TryConstructPeer(Java.Interop.IJavaPeerable! self, ref Java.Interop.JniObjectReference reference, Java.Interop.JniObjectReferenceOptions options, System.Type! type) -> bool

0 commit comments

Comments
 (0)