I am storing off JNIEnv
in a global so I can call static java methods later. But is it nessasary to store off a global pointer to the JNIEnv
, they way one would with any other java object, or is it a special case that does not require this.
JNIEnv* globalEnvPointer;
[JNICALL etc] void init(JNIENv* env, [etc])
{
//required?
globalEnvPointer = (JNIENv*) (env*)->GetGlobalRef(env, env);
//or is this OK?
globalEnvPointer = env;
}
Edit
I'm bing a bit dumb here, all the methods that will use globalEnvPointer
, are invoked within my init because my init
is actually my c
program's main
method, which won't return until the end of the program. I am also using no other threads in the c program. I think this simplifies the answer.
JNIEnv* globalEnvPointer;
[JNICALL etc] void main(JNIENv* env, [etc])
{
//required?
globalEnvPointer = (JNIENv*) (env*)->GetGlobalRef(env, env);
//or is this OK?
globalEnvPointer = env;
someMethod();
}
void someMethod()
{
//use globalEnvPointer here
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…