Passing, storing an retrieving an object reference in JNI
843829Oct 4 2006 — edited Oct 5 2006Hi All,
I am trying out a native code which accepts as the input parameter, a reference to a java object. This reference is stored into a static variable inside the native code, and retrieved from another java thread.
In short - the code in Java looks like
MyObject d1 = new MyObject( );
d1.asyncFun();
In asyncFun, the native method is called with a self reference
public void asyncFun()
{
asyncFunNative( this );
}
The native code looks like this:
static jobject reference;
JNIEXPORT void JNICALL Java_MyObject_asyncFunNative
(JNIEnv *env, jobject obj1, jobject objref)
{
reference = objref;
}
There is another native function, in the same file that does this:
JNIEXPORT jobject JNICALL Java_Retriever_receiveObject
(JNIEnv *env, jobject obj1)
{
...decalre the cls, method variables etc...
myEvntCls = (*env)->FindClass(env, "MyEvent");
/* Find method id for constructor */
myEvntId = (*env)->GetMethodID(env, myEvntCls, "<init>", "(LMyObject;)V");
/* Create an object of type MyEvent */
myEvntObj = (*env)->NewObject(env, myEvntCls, myEvntId, (jobject)reference);
return(myEvntObj);
}
the problem that iface is that when receiveObject is called from a java thread, the object that gets passed in to the constructor of MyEvent is NULL.
can someone please explain why this is so?
Thanks,
Pritel