Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Passing, storing an retrieving an object reference in JNI

843829Oct 4 2006 — edited Oct 5 2006
Hi 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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 2 2006
Added on Oct 4 2006
5 comments
615 views