JNI caching of jclass

S

Simon

Hi,

Sec. 4.4 of the JNI tutorial says that it is a good idea to cache field and
method IDs. I tried to also cache the jclass's which seemed to work in the
beginning but crashed the application later. I noticed that the jmethodID's are
the same every time I retrieve them but the jclass's are different (though I am
not sure how to compare them properly). If I use (*env)->FindClass(...)
everytime, everything works fine. Did anyone observe similar problems already?

I'm not providing my source code here because I don't think it helps much. I do
it almost exactly as described in Sect 4.4.2 "Caching in the Defining Class's
Initializer").

Cheers,
Simon
 
C

Chris Uppal

Simon said:
I tried to also cache the jclass's which seemed to work
in the beginning but crashed the application later. I noticed that the
jmethodID's are the same every time I retrieve them but the jclass's are
different (though I am not sure how to compare them properly). If I use
(*env)->FindClass(...) everytime, everything works fine. Did anyone
observe similar problems already?

FindClass() answers a local reference which is only valid until the JNI code
returns (assuming that your JNI code is being called from Java rather than the
other way around).

You can obtain a long-lived reference by converting into a global reference.
See chapter 5 of the JNI book. Downloadable from:

http://java.sun.com/docs/books/jni/index.html

-- chris
 
G

Gordon Beaton

Sec. 4.4 of the JNI tutorial says that it is a good idea to cache
field and method IDs. I tried to also cache the jclass's which
seemed to work in the beginning but crashed the application later. I
noticed that the jmethodID's are the same every time I retrieve them
but the jclass's are different (though I am not sure how to compare
them properly). If I use (*env)->FindClass(...) everytime,
everything works fine. Did anyone observe similar problems already?

I think you are referring to the Liang book, not the JNI tutorial.

Whenever you cache a reference to any object (and classes are objects
too), it must be a global reference. Use NewGlobalRef() for that, but
don't forget to use DeleteGlobalRef() when you no longer need the
saved reference.

/gordon
 
S

Simon

I think you are referring to the Liang book, not the JNI tutorial.

Yes, you are right.
Whenever you cache a reference to any object (and classes are objects
too), it must be a global reference. Use NewGlobalRef() for that, but
don't forget to use DeleteGlobalRef() when you no longer need the
saved reference.

That seems to fix the problem. Thanks to both of you and sorry for asking a
question that is answered in the book. I just looked in the wrong section.

Cheers,
Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,685
Members
48,796
Latest member
Greg L.

Latest Threads

Top