Bug 726821 - Caching JNI class references should use a global reference. r=blassey
authorGian-Carlo Pascutto <gpascutto@mozilla.com>
Mon, 27 Feb 2012 12:28:22 +0100
changeset 87834 bebeca2270ee23849c85b18707900dcf273fc3b3
parent 87833 0bf80d3cebf5d8cdc251d2c4432651350eddc3c7
child 87835 fdd55a46661c761dcf681e5b74bec59f11c3a155
push id22160
push usermbrubeck@mozilla.com
push dateTue, 28 Feb 2012 17:21:33 +0000
treeherdermozilla-central@dde4e0089a18 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersblassey
bugs726821
milestone13.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 726821 - Caching JNI class references should use a global reference. r=blassey
mozglue/android/SQLiteBridge.cpp
--- a/mozglue/android/SQLiteBridge.cpp
+++ b/mozglue/android/SQLiteBridge.cpp
@@ -116,27 +116,44 @@ JNI_Throw(JNIEnv* jenv, const char* name
     jenv->DeleteLocalRef(cls);
 }
 
 static void
 JNI_Setup(JNIEnv* jenv)
 {
     if (initialized) return;
 
-    objectClass       = jenv->FindClass("java/lang/Object");
-    stringClass       = jenv->FindClass("java/lang/String");
-    byteBufferClass   = jenv->FindClass("java/nio/ByteBuffer");
-    cursorClass       = jenv->FindClass("org/mozilla/gecko/sqlite/MatrixBlobCursor");
+    jclass lObjectClass       = jenv->FindClass("java/lang/Object");
+    jclass lStringClass       = jenv->FindClass("java/lang/String");
+    jclass lByteBufferClass   = jenv->FindClass("java/nio/ByteBuffer");
+    jclass lCursorClass       = jenv->FindClass("org/mozilla/gecko/sqlite/MatrixBlobCursor");
+
+    if (lStringClass == NULL
+        || lObjectClass == NULL
+        || lByteBufferClass == NULL
+        || lCursorClass == NULL) {
+        LOG("Error finding classes");
+        JNI_Throw(jenv, "org/mozilla/gecko/sqlite/SQLiteBridgeException",
+                  "FindClass error");
+        return;
+    }
+
+    // Those are only local references. Make them global so they work
+    // across calls and threads.
+    objectClass = (jclass)jenv->NewGlobalRef(lObjectClass);
+    stringClass = (jclass)jenv->NewGlobalRef(lStringClass);
+    byteBufferClass = (jclass)jenv->NewGlobalRef(lByteBufferClass);
+    cursorClass = (jclass)jenv->NewGlobalRef(lCursorClass);
 
     if (stringClass == NULL || objectClass == NULL
         || byteBufferClass == NULL
         || cursorClass == NULL) {
-        LOG("Error finding classes");
+        LOG("Error getting global references");
         JNI_Throw(jenv, "org/mozilla/gecko/sqlite/SQLiteBridgeException",
-                  "FindClass error");
+                  "NewGlobalRef error");
         return;
     }
 
     // public static ByteBuffer allocateDirect(int capacity)
     jByteBufferAllocateDirect =
         jenv->GetStaticMethodID(byteBufferClass, "allocateDirect", "(I)Ljava/nio/ByteBuffer;");
     // new MatrixBlobCursor(String [])
     jCursorConstructor =