Search code examples
c++android-ndkjava-native-interface

JNI / Android NDK - Maintaining a global object reference


I know I can't keep a reference to the internals of an array so I was wondering if it is OK to keep a global pointer to a java array object or indeed any java object. And whether it makes any difference that I create it from the C++.

It works, but I was worried the garbage collector could potentially relocate the memory (which I understand it the reason for Get... and Release... methods on JNIEnv).

//global jfloatArray
jfloatArray jarray;

//called once
JNIEXPORT void Java_com_example_test1_Main_Init
  (JNIEnv *env, jclass thiz){
    //create once
    jarray = env->NewFloatArray(10);  //if valid, would it be as valid to pass it in?
}

//called repeatedly
JNIEXPORT void JNICALL
Java_com_example_test1_Main_loop(JNIEnv* env, jobject thiz) {    
    //use jarray in here
}

Edit:

Here is the correct code.

//global jfloatArray
jfloatArray jarray;

//called once
JNIEXPORT void Java_com_example_test1_Main_Init
  (JNIEnv *env, jclass thiz){
      //create once
      //create it - this gives a local reference
      jfloatArray local_jarray = env->NewFloatArray(10);
      //get a global reference, cast it and set to the global "jarray"
      jarray = (jfloatArray) env->NewGlobalRef(local_jarray);
      //delete the local reference
      env->DeleteLocalRef(local_jarray);
}

//called repeatedly
JNIEXPORT void JNICALL
Java_com_example_test1_Main_loop(JNIEnv* env, jobject thiz) {    
    //use jarray in here
}

Solution

  • Your reference is merely that -- a reference. It will not prevent the object it refers to from being relocated. It will prevent the object from being recollected; local references are automatically destroyed after returning, but since you're using a global variable, you should use a global reference, which necessitates manual management. See NewGlobalRef and DeleteGlobalRef.