NewGlobalRef
jobject NewGlobalRef(JNIEnv *env, jobject obj);
Creates a new global reference to the object referred to by the
obj
argument. The obj
argument may be a global or local reference. Global references must be explicitly disposed of by calling DeleteGlobalRef()
.LINKAGE:
Index 21 in the JNIEnv interface function table.
PARAMETERS of NewGlobalRef
env
: the JNI interface pointer.obj
: a global or local reference.RETURNS:
Returns a global reference, or
NULL
if the system runs out of memory.Example - NewGlobalRef
/* This code is OK */ jstring MyNewString(JNIEnv *env, jchar *chars, jint len) { static jclass stringClass = NULL; ... if (stringClass == NULL) { jclass localRefCls = (*env)->FindClass(env, "java/lang/String"); if (localRefCls == NULL) { return NULL; /* exception thrown */ } /* Create a global reference */ stringClass = (*env)->NewGlobalRef(env, localRefCls); /* The local reference is no longer useful */ (*env)->DeleteLocalRef(env, localRefCls); /* Is the global reference created successfully? */ if (stringClass == NULL) { return NULL; /* out of memory exception thrown */ } } ... }