Skip to content
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
27 changes: 11 additions & 16 deletions src/Java.Interop/Java.Interop/JniEnvironment.Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,38 +114,33 @@ static unsafe bool TryLoadClassWithFallback (JniEnvironmentInfo info, IntPtr thr
{
result = default;

var findClassThrown = new JniObjectReference (thrown, JniObjectReferenceType.Local);
LogCreateLocalRef (findClassThrown);
Exception? pendingException = info.Runtime.GetExceptionForThrowable (ref findClassThrown, JniObjectReferenceOptions.CopyAndDispose);

if (Class_forName.IsValid) {
var __args = stackalloc JniArgumentValue [3];
__args [0] = new JniArgumentValue (classNameJavaString);
__args [1] = new JniArgumentValue (true); // initialize the class
__args [2] = new JniArgumentValue (info.Runtime.ClassLoader);

var c = RawCallStaticObjectMethodA (info.EnvironmentPointer, out thrown, Class_reference.Handle, Class_forName.ID, (IntPtr) __args);
if (thrown == IntPtr.Zero) {
(pendingException as IJavaPeerable)?.Dispose ();
var c = RawCallStaticObjectMethodA (info.EnvironmentPointer, out var forNameThrown, Class_reference.Handle, Class_forName.ID, (IntPtr) __args);
if (forNameThrown == IntPtr.Zero) {
// Class.forName() succeeded; discard the FindClass throwable.
JniEnvironment.References.RawDeleteLocalRef (info.EnvironmentPointer, thrown);
result = new JniObjectReference (c, JniObjectReferenceType.Local);
JniEnvironment.LogCreateLocalRef (result);
return true;
}
RawExceptionClear (info.EnvironmentPointer);

if (pendingException != null) {
JniEnvironment.References.RawDeleteLocalRef (info.EnvironmentPointer, thrown);
} else {
var loadClassThrown = new JniObjectReference (thrown, JniObjectReferenceType.Local);
LogCreateLocalRef (loadClassThrown);
pendingException = info.Runtime.GetExceptionForThrowable (ref loadClassThrown, JniObjectReferenceOptions.CopyAndDispose);
}
JniEnvironment.References.RawDeleteLocalRef (info.EnvironmentPointer, forNameThrown);
}

if (!throwOnError) {
(pendingException as IJavaPeerable)?.Dispose ();
JniEnvironment.References.RawDeleteLocalRef (info.EnvironmentPointer, thrown);
return false;
}

// Both FindClass and Class.forName() failed; materialize a managed exception to throw.
var findClassThrown = new JniObjectReference (thrown, JniObjectReferenceType.Local);
LogCreateLocalRef (findClassThrown);
Exception? pendingException = info.Runtime.GetExceptionForThrowable (ref findClassThrown, JniObjectReferenceOptions.CopyAndDispose);
Comment thread
simonrozsival marked this conversation as resolved.
if (pendingException != null)
throw pendingException;

Expand Down
20 changes: 20 additions & 0 deletions tests/Java.Interop-Tests/Java.Interop/JniTypeUtf8Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ public void TryFindClass_Utf8 ()
Assert.IsFalse (notFound.IsValid);
}

[Test]
public void TryFindClass_Utf8_DoesNotLeakGlobalRefs ()
{
int grefsBefore = JniEnvironment.Runtime.GlobalReferenceCount;
JniEnvironment.Types.TryFindClass ("does/not/Exist"u8, out _);
int grefsAfter = JniEnvironment.Runtime.GlobalReferenceCount;
Assert.AreEqual (grefsBefore, grefsAfter,
"TryFindClass for non-existent classes should not leak global references");
}

[Test]
public void TryFindClass_String_DoesNotLeakGlobalRefs ()
{
int grefsBefore = JniEnvironment.Runtime.GlobalReferenceCount;
JniEnvironment.Types.TryFindClass ("does/not/Exist", out _);
int grefsAfter = JniEnvironment.Runtime.GlobalReferenceCount;
Assert.AreEqual (grefsBefore, grefsAfter,
"TryFindClass for non-existent classes should not leak global references");
}

[Test]
public void GetMethodID_Utf8_MatchesStringOverload ()
{
Expand Down