Skip to content

Commit 88731f9

Browse files
authored
Merge pull request #857 from NativeScript/pete/api26-crash
Replace SparseArray with HashMap to hold JNI instances
2 parents 2941625 + 2e3db9a commit 88731f9

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

runtime/src/main/java/com/tns/Runtime.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ void passUncaughtExceptionToJs(Throwable ex, String stackTrace) {
8080
"Primitive types need to be manually wrapped in their respective Object wrappers.\n" +
8181
"If you are creating an instance of an inner class, make sure to always provide reference to the outer `this` as the first argument.";
8282

83-
private final SparseArray<Object> strongInstances = new SparseArray<Object>();
83+
private final HashMap<Integer, Object> strongInstances = new HashMap<>();
8484

85-
private final SparseArray<WeakReference<Object>> weakInstances = new SparseArray<WeakReference<Object>>();
85+
private final HashMap<Integer, WeakReference<Object>> weakInstances = new HashMap<>();
8686

8787
private final NativeScriptHashMap<Object, Integer> strongJavaObjectToID = new NativeScriptHashMap<Object, Integer>();
8888

@@ -801,7 +801,7 @@ private void makeInstanceWeak(int javaObjectID, boolean keepAsWeak) {
801801
weakInstances.put(javaObjectID, new WeakReference<Object>(instance));
802802
}
803803

804-
strongInstances.delete(javaObjectID);
804+
strongInstances.remove(javaObjectID);
805805
strongJavaObjectToID.remove(instance);
806806
}
807807

@@ -829,17 +829,17 @@ private boolean makeInstanceWeakAndCheckIfAlive(int javaObjectID) {
829829
if (instance == null) {
830830
// The Java was moved from strong to weak, and then the Java instance was collected.
831831
weakInstances.remove(javaObjectID);
832-
weakJavaObjectToID.remove(Integer.valueOf(javaObjectID));
832+
weakJavaObjectToID.remove(javaObjectID);
833833
return false;
834834
} else {
835835
return true;
836836
}
837837
}
838838
} else {
839-
strongInstances.delete(javaObjectID);
839+
strongInstances.remove(javaObjectID);
840840
strongJavaObjectToID.remove(instance);
841841

842-
weakJavaObjectToID.put(instance, Integer.valueOf(javaObjectID));
842+
weakJavaObjectToID.put(instance, javaObjectID);
843843
weakInstances.put(javaObjectID, new WeakReference<Object>(instance));
844844

845845
return true;
@@ -862,7 +862,7 @@ private void checkWeakObjectAreAlive(ByteBuffer input, ByteBuffer output, int le
862862

863863
if (instance == null) {
864864
isReleased = 1;
865-
weakInstances.delete(javaObjectId);
865+
weakInstances.remove(javaObjectId);
866866
} else {
867867
isReleased = 0;
868868
}
@@ -880,7 +880,11 @@ private Object getJavaObjectByID(int javaObjectID) throws Exception {
880880
logger.write("Platform.getJavaObjectByID:" + javaObjectID);
881881
}
882882

883-
Object instance = strongInstances.get(javaObjectID, keyNotFoundObject);
883+
Object instance = strongInstances.get(javaObjectID);
884+
885+
if (instance == null) {
886+
instance = keyNotFoundObject;
887+
}
884888

885889
if (instance == keyNotFoundObject) {
886890
WeakReference<Object> wr = weakInstances.get(javaObjectID);

0 commit comments

Comments
 (0)