Another attempt at fixing JNI local reference leaks.
The thread making the call here may never
make it into the virtual machine, so the
local reference is explicitly deleted.
Bug: 4383243
Change-Id: I44d546f7f71a4fb2631133d65573cee19d466fb6
diff --git a/jni/com_android_nfc.cpp b/jni/com_android_nfc.cpp
index 6187c13..c9b9da1 100644
--- a/jni/com_android_nfc.cpp
+++ b/jni/com_android_nfc.cpp
@@ -560,23 +560,35 @@
}
}
}
+
// Build the Java arrays
- *techList = e->NewIntArray(index);
- *handleList = e->NewIntArray(index);
- *libnfcTypeList = e->NewIntArray(index);
-
- jint* techItems = e->GetIntArrayElements(*techList, NULL);
- jint* handleItems = e->GetIntArrayElements(*handleList, NULL);
- jint* typeItems = e->GetIntArrayElements(*libnfcTypeList, NULL);
- for (int i = 0; i < index; i++) {
- techItems[i] = technologies[i];
- handleItems[i] = handles[i];
- typeItems[i] = libnfctypes[i];
+ if (techList != NULL) {
+ *techList = e->NewIntArray(index);
+ jint* techItems = e->GetIntArrayElements(*techList, NULL);
+ for (int i = 0; i < index; i++) {
+ techItems[i] = technologies[i];
+ }
+ e->ReleaseIntArrayElements(*techList, techItems, 0);
}
- e->ReleaseIntArrayElements(*techList, techItems, 0);
- e->ReleaseIntArrayElements(*handleList, handleItems, 0);
- e->ReleaseIntArrayElements(*libnfcTypeList, typeItems, 0);
+ if (handleList != NULL) {
+ *handleList = e->NewIntArray(index);
+ jint* handleItems = e->GetIntArrayElements(*handleList, NULL);
+ for (int i = 0; i < index; i++) {
+ handleItems[i] = handles[i];
+ }
+ e->ReleaseIntArrayElements(*handleList, handleItems, 0);
+ }
+
+ if (libnfcTypeList != NULL) {
+ *libnfcTypeList = e->NewIntArray(index);
+
+ jint* typeItems = e->GetIntArrayElements(*libnfcTypeList, NULL);
+ for (int i = 0; i < index; i++) {
+ typeItems[i] = libnfctypes[i];
+ }
+ e->ReleaseIntArrayElements(*libnfcTypeList, typeItems, 0);
+ }
}
} // namespace android
diff --git a/jni/com_android_nfc_NativeNfcSecureElement.cpp b/jni/com_android_nfc_NativeNfcSecureElement.cpp
index 1e70839..f309122 100755
--- a/jni/com_android_nfc_NativeNfcSecureElement.cpp
+++ b/jni/com_android_nfc_NativeNfcSecureElement.cpp
@@ -138,10 +138,7 @@
/* Set type name */
jintArray techList;
- jintArray handleList;
- jintArray typeList;
- nfc_jni_get_technology_tree(e, psRemoteDevList,uNofRemoteDev, &techList,
- &handleList, &typeList);
+ nfc_jni_get_technology_tree(e, psRemoteDevList,uNofRemoteDev, &techList, NULL, NULL);
// TODO: Should use the "connected" technology, for now use the first
if ((techList != NULL) && e->GetArrayLength(techList) > 0) {
@@ -157,6 +154,10 @@
LOGE("Discovered secure element, but could not resolve tech");
status = NFCSTATUS_FAILED;
}
+
+ // This thread may not return to the virtual machine for a long time
+ // so make sure to delete the local refernce to the tech list.
+ if (techList != NULL) e->DeleteLocalRef(techList);
}
pContextData->status = status;