Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

JNI caching of jclass

Thread view: 
Simon - 12 Jan 2006 14:54 GMT
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
Chris Uppal - 12 Jan 2006 15:19 GMT
> 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
Gordon Beaton - 12 Jan 2006 15:32 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.