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

[Mono.Android] fix leaking lrefs in TypeManager #9136

Merged
merged 1 commit into from
Jul 25, 2024
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: 16 additions & 11 deletions src/Mono.Android/Java.Interop/TypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,21 +322,26 @@ internal static IJavaPeerable CreateInstance (IntPtr handle, JniHandleOwnership
if (!typeSig.IsValid || typeSig.SimpleReference == null) {
throw new ArgumentException ($"Could not determine Java type corresponding to `{type.AssemblyQualifiedName}`.", nameof (targetType));
}
JniObjectReference typeClass;

JniObjectReference typeClass = default;
JniObjectReference handleClass = default;
try {
typeClass = JniEnvironment.Types.FindClass (typeSig.SimpleReference);
} catch (Exception e) {
throw new ArgumentException ($"Could not find Java class `{typeSig.SimpleReference}`.",
nameof (targetType),
e);
}
try {
typeClass = JniEnvironment.Types.FindClass (typeSig.SimpleReference);
} catch (Exception e) {
throw new ArgumentException ($"Could not find Java class `{typeSig.SimpleReference}`.",
nameof (targetType),
e);
}

var handleClass = JniEnvironment.Types.GetObjectClass (new JniObjectReference (handle));
if (!JniEnvironment.Types.IsAssignableFrom (handleClass, typeClass)) {
Logger.Log (LogLevel.Info, "*jonp*", $"# jonp: can't assign `{GetClassName(handleClass.Handle)}` to `{GetClassName(typeClass.Handle)}`");
handleClass = JniEnvironment.Types.GetObjectClass (new JniObjectReference (handle));
if (!JniEnvironment.Types.IsAssignableFrom (handleClass, typeClass)) {
Logger.Log (LogLevel.Info, "*jonp*", $"# jonp: can't assign `{GetClassName(handleClass.Handle)}` to `{GetClassName(typeClass.Handle)}`");
return null;
}
} finally {
JniObjectReference.Dispose (ref handleClass);
JniObjectReference.Dispose (ref typeClass);
return null;
}

IJavaPeerable? result = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using Android.App;
using Android.Content;
using NUnit.Framework;

namespace Android.ContentTests;

[TestFixture]
public class SharedPreferencesTest
{
const string Name = "testpreferences";
const int Count = 1000;

ISharedPreferences GetPreferences () =>
Application.Context.GetSharedPreferences (Name, FileCreationMode.Private);

// NOTE: test case on API 23 can trigger:
// art/runtime/indirect_reference_table.cc:115] JNI ERROR (app bug): local reference table overflow
[Test]
public void PutAndGetManyValues ()
{
for (int i = 0; i < Count; i++) {
using var prefs = GetPreferences ();
using var editor = prefs.Edit ();
editor.PutString ("key" + i, "value" + i);
editor.Apply ();
}

for (int i = 0; i < Count; i++) {
using var prefs = GetPreferences ();
Assert.AreEqual ("value" + i, prefs.GetString ("key" + i, null));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Android.App\ApplicationTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.Content.Res\AssetManagerTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.Content\IntentTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.Content\SharedPreferencesTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.Graphics\NinePatchTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.Runtime\AndroidEnvironmentTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.Runtime\CharSequenceTest.cs" />
Expand Down
Loading