JNI global reference in native code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • etiainen
    New Member
    • Aug 2007
    • 40

    JNI global reference in native code

    Hello everyone, I have a question:
    is there a way to make a global reference (pointer) in native code that'll survive between native method invocations?

    something like this:
    Code:
    ...
    Some_struct *something=NULL;
    
    JNIEXPORT void JNICALL Java_SomeClass_initSomething(JNIEnv *env, jobject obj);
    
    JNIEXPORT jint JNICALL Java_SomeClass_useSomething(JNIEnv *env, jobject obj);
    ...
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Does this help?

    kind regards,

    Jos

    Comment

    • etiainen
      New Member
      • Aug 2007
      • 40

      #3
      Well, it does a little bit...
      The thing is that I don't have a matching java type for the structure of the native variable. I don't know if GlobalRef can refer to something completely native without knowing about its structure. But here's a bright idea, I'll try holding that reference as void* and casting it in native calls...

      Thanks Jos!

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by etiainen
        Well, it does a little bit...
        The thing is that I don't have a matching java type for the structure of the native variable. I don't know if GlobalRef can refer to something completely native without knowing about its structure. But here's a bright idea, I'll try holding that reference as void* and casting it in native calls...

        Thanks Jos!
        It's even simpler; I read a bit more and found this; it basically means that the JVM
        leaves your allocated structs alone. All you need to do is keep a pointer to it
        somewhere; you can keep this pointer yourself in a global variable or you can make
        the JVM keep it (in an int or long in which case you have to cast the value).

        kind regards,

        Jos

        Comment

        Working...