This discussion is archived
3 Replies Latest reply: May 18, 2012 5:15 PM by EJP RSS

Caching values in JNI having multiple instances of the Java wrapper object

938267 Newbie
Currently Being Moderated
Hi,

I have a Java lib that wraps and more complex 3 ^rd^ party C++ lib using JNI. This C++ lib is asynchronous. So, there's the need to register callbacks. I've already done this. I can register a callback from Java to the C++ lib and I can get notified, also in Java, when the C++ lib fires an event.

To achieve this I had to cache some information in my "JNI glue code". I've cached a JavaVM* and a jobject to that I can call a Java method in "glue code" callback. Something like this:

-----
// This is supposed to be a callback registered in the 3 ^rd^ party lib's object that I'm wrapping
void callback_OnSomething(int someInfo) {
// Call a the "fireOnSomething" method from Java
}
-----

So, all of this is working, but I have a small problem. When I have more than one instance of my "Java wrapper object", my cached values in the glue code will get mixed up, because "glue code" cached variables are the same.

Have you ever done something like this? How have you solved it?

Many thanks in advance.
  • 1. Re: Caching values in JNI having multiple instances of the Java wrapper object
    EJP Guru
    Currently Being Moderated
    I don't know what you mean by 'glued code', but you can't save jobjects across JNI method invocations unless you convert them to GlobalReferences.

    It sounds like you need to allocate a C++ callback object per Java object and keep your state in there. You can associate it with the Java object via a long to keep its address in. Make sure to delete it when finished with it.
  • 2. Re: Caching values in JNI having multiple instances of the Java wrapper object
    938267 Newbie
    Currently Being Moderated
    Hi,

    Thanks for you answer! By "glue code" I mean the C code that's used to "glue" the Java code to the 3 ^rd^ party lib. I've seen this term here: en.wikipedia.org/wiki/Glue_code.

    I've already converted my objects to "GlobalReferences". My example is running perfectly when I have just one instance of the Java wrapper object.

    So, if I understood it correctly you're suggesting that I cache the values in the Java object itself? So, what I have to do is to just have a "long" in my Java object with the memory addresses. And is the "long" always valid, whether I'm running a 32 bits or 64 bits version?

    Just one more thing. When you say "Make sure to delete it when finished with it", what do you mean?

    Once again, many thanks!
  • 3. Re: Caching values in JNI having multiple instances of the Java wrapper object
    EJP Guru
    Currently Being Moderated
    So, if I understood it correctly you're suggesting that I cache the values in the Java object itself?
    No, i said 'allocate a C++ object'. Store its address in the Java object. A long is long enough. Delete it when you are done with it or you have a memory leak.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points