从下面的代码片段中,我尝试在块的本地范围内声明它后进行测试NewGlobalRef
并尝试使其成为全局。clsStr
if
jstring Java_Package_LocalAndGlobalReference_returnGlobalReference
(JNIEnv *env, jobject obj) {
if(1) {
printf("In function make global reference\n");
jclass clsStr ;
jclass cls = (*env)->FindClass(env,"java/lang/String");
if( cls == NULL)
return NULL;
// create a global reference of clsStr
clsStr = (*env)->NewGlobalRef(env,cls);
// Delete the local reference, which is no longer userful
(*env)->DeleteLocalRef(env,cls);
if(clsStr == NULL)
return NULL;
}
return clsStr; // statement 31
}
当我运行上面的代码片段时,我收到以下错误:
W:\elita\jnitesters\workspace\c\LGR\LGR.c:31: error: 'clsStr' undeclared (first use in this function)
W:\elita\jnitesters\workspace\c\LGR\LGR.c:31: error: (Each undeclared identifier is reported only once
W:\elita\jnitesters\workspace\c\LGR\LGR.c:31: error: for each function it appears in.)
clsStr
当我使用 satement 将 var 设为全局时,为什么我会收到未定义的错误消息clsStr = (*env)->NewGlobalRef(env,cls)
?