> 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
> 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
[quoted text clipped - 3 lines]
> 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

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Simon - 13 Jan 2006 08:02 GMT
> 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