I'm trying to use JNI in android to make a function pointer that a native library I'm using uses forward it's call to java.
When initializeStateController
is called, a new thread is made using pthread_create
that calls the function pointer whenever the state of the State Controller changes.
However, when I try to call GetStaticMethodID
from state_exec
in C, I'm getting the following error:
JNI DETECTED ERROR IN APPLICATION: jclass is an invalid local reference: 0xec500019 (0xdead4321) in call to GetStaticMethodID
This is my current code:
C Code
state_controller *controller;
JavaVM* gJvm = NULL;
static jclass sc_class;
JNIEnv* getEnv() {
JNIEnv *env;
int status = (*gJvm)->GetEnv(gJvm, (void**)&env, JNI_VERSION_1_6);
if(status < 0) {
status = (*gJvm)->AttachCurrentThread(gJvm, &env, NULL);
if(status < 0) {
return NULL;
}
}
return env;
}
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* pjvm, void* reserved) {
gJvm = pjvm;
JNIEnv *env = getEnv();
sc_class = (*env)->FindClass(env, "com/my/package/StateController");
return JNI_VERSION_1_6;
}
int state_exec(int state, int from) {
JNIEnv *env = getEnv();
jmethodID mid = (*env)->GetStaticMethodID(env, sc_class, "stateExec","(II)I");
jint result = (*env)->CallStaticIntMethod(env, sc_class, mid, state, from);
return (int) result;
}
// This part is unimportant.
// This is just where I hand the function pointer
// to the library to use.
JNIEXPORT void JNICALL
Java_com_my_package_StateController_initializeStateController
(
JNIEnv *env,
jobject jobj
) {
controller = new_state_controller(
state_exec
);
sc_start_controller(controller);
}
Java
package com.my.package;
class StateController {
public native void initializeStateController();
public static int stateExec(int state, int from) {
Log.d("StateTest", "State " + state + " -> " + from);
return 0;
}
}
. . .
(new StateController()).initializeStateController();