Saturday, April 20, 2013

jni jnienv NewWeakGlobalRef example c c++ java


NewWeakGlobalRef

jweak NewWeakGlobalRef(JNIEnv *env, jobject obj);
Creates a new weak global reference. Returns NULL if obj refers to null, or if the VM runs out of memory. If the VM runs out of memory, an OutOfMemoryError will be thrown. (NewWeakGlobalRef)
LINKAGE:
Index 226 in the JNIEnv interface function table.
SINCE:
JDK/JRE 1.2
Example - NewWeakGlobalRef

 JNIEXPORT void JNICALL
 Java_mypkg_MyCls_f(JNIEnv *env, jobject self)
 {
     static jclass myCls2 = NULL;
     if (myCls2 == NULL) {
         jclass myCls2Local =
             (*env)->FindClass(env, "mypkg/MyCls2");
         if (myCls2Local == NULL) {
             return; /* can't find class */
         }
         myCls2 = NewWeakGlobalRef(env, myCls2Local);
         if (myCls2 == NULL) {
             return; /* out of memory */
         }
     }
     ... /* use myCls2 */
 }