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 / September 2006

Tip: Looking for answers? Try searching our database.

JNI, callbacks and remembering the global reference

Thread view: 
Bill Medland - 28 Sep 2006 23:05 GMT
Is there an easier way to do this?

I am writing a library layer of code that uses JNI to call down into a
library implemented in C.

The C library includes a callback mechanism including both register and
unregister;
e.g. registerCallback(functiontype func, void *context);
unregisterCallback(functiontype func, void *context);

The java library layer will export a similar ability to add and remove a
listener.

The JNI layer will pass the listener as the context and the address of a
small redirector function as the func arguments.

So clearly I will have to call NewGlobalRef on the jobject passed down as
the listener.
(Presumably that global reference will be valid in different environments
when it comes back to the redirector)

The problem is with removing the listener; how am I going to figure out what
jobject to pass to the DeleteGlobalRef.

It seems to me that the JNI layer is going to have to keep a copy of all the
global references generated and then search that list using IsSameObject to
find the global reference that was used that refers to the same object as
the local reference being passed down.

Is there an easier way?  Like is there a way to find the global reference
corresponding to a local reference?
Signature

Bill Medland

Gordon Beaton - 29 Sep 2006 07:07 GMT
> It seems to me that the JNI layer is going to have to keep a copy of
> all the global references generated and then search that list using
[quoted text clipped - 3 lines]
> Is there an easier way? Like is there a way to find the global
> reference corresponding to a local reference?

Nothing forces you to use the reference itself as a key. After
registering the listener, return a value that the caller can use to
deregister it later.

A suitable value might an index into an array where you keep the
listener references. Or you could pass the actual address of a C
struct (cast to long) that holds the reference itself and other
information about the listener you might be storing.

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e



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



©2008 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.