I have two JNI functions. The first one allows the programmer to pass an arbitrary Java object to C++:
void Java_org_love_Lua_pushLightUserdata( JNIEnv* env, jobject javaThis, jlong state, jobject value )
{
jobject* objectRef = lua_newuserdata( (lua_State*)state, sizeof( jobject ) );
*objectRef = (*env)->NewGlobalRef( env, value );
}
Then there is another that retrieves this object:
jobject Java_org_love_Lua_toUserdata( JNIEnv* env, jobject javaThis, jlong state, jint index )
{
jobject obj = *(jobject*)lua_touserdata( (lua_State*)state, (int)index );
return obj;
}
However, for some reason this function always returns a null value to Java. I know that the jobject itself is good, because I’m also using this method for a callback function. This is the first time I’m actually trying to return an object, though. What am I doing wrong?
Because you aren’t returning the global ref that you carefully created, but rather perhaps a long-stale local ref?