What is the lifetime of JNI localrefs for C++ calling Java?

Michael Anderson

When Java is calling into a C function via JNI, we know that any local references to classes or objects may become invalid once the C function returns to java.

However when we only have C calling into Javam there is no "return to java". So it's not clear to me when these references will become invalid.

For example this is bad since aClass, and anObject will potentially become invalid once the call is complete.

jclass aClass;
jobject anObject;

JNIEXPORT void JNICALL Java_example_method1(JNIEnv *env) {
    aClass = env->FindClass("AClass");
}

JNIEXPORT void JNICALL Java_example_method2(JNIEnv * env) {
    jmethodID constructor = env->GetMethodID( aClass, "<init>", "()V");
    anObject = env->NewObject(klass, constructor );
}

However in the following example, what can cause aClass and anObject to become invalid? Is it only an explicit call to DeleteLocalRef?

jclass aClass;
jobject anObject;

int main() {
        JavaVMInitArgs vm_args;
        JavaVMOption options[1];
        vm_args.version = JNI_VERSION_1_6;
        vm_args.nOptions = 1;
        options[0].optionString = "-Djava.class.path=.";
        vm_args.options = options;
        vm_args.ignoreUnrecognized = JNI_FALSE;
        JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);

        aClass = env->FindClass("AClass");
        jmethodID constructor = env->GetMethodID( aClass, "<init>", "()V");
        anObject = env->NewObject(klass, constructor );
}
Marquis of Lorne

That's correct. When Java calls JNI it allocates a table where local refs to, that are all released when the JNI returns. Wen you're calling Java there is no such mechanism.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a Java variadic function from C through the JNI

From Dev

JNI calling Java from C++ with multiple threads

From Dev

JNI: Calling a java method from C periodically is not working

From Dev

Java JNI NullPointerException after calling method from C with valid pointers

From Dev

Calling RenderScript from C / JNI

From Dev

What's "Method Signature" parameter when calling a Java method using JNI?

From Dev

Calling a static void Java method from JNI

From Dev

JNI Calling Java Method With Array Parameter

From Dev

Calling JNI C function in another source file

From Dev

Calling dll implemented JNI from C++

From Dev

Calling C system calls from JNI

From Dev

JNI C++ UnsatisfiedLinkError When Calling Method

From Dev

Calling java function from c++ using jni: Failed to find static method id

From Dev

Error while calling C# from java using jni4net from 64 bit Os

From Dev

Build a dll in C++ for java which is calling another dll (JNI on Eclipse)

From Java

What is the lifetime of unassigned object created in a constructor in java?

From Dev

What is the lifetime of an ejb client java ee 7

From Java

JNI: From C code to Java and JNI

From

What is the lifetime of a static variable in a C++ function?

From Java

JNI and Java: ant calling make or make calling ant?

From Java

Java C++ without JNI

From Java

What happens if I call a java function from multiple threads from C with JNI?

From Java

JNI, Garbage collection and Pointers- Java/C++ who should do what?

From Dev

Calling Java Methods from JNI results in program crash

From Java

Calling into a saved java object via JNI from a different thread

From Dev

Calling a Java Method from the native code using jni

From Dev

JNI vs JNA, calling Java from Fortran95

From Dev

What is the lifetime of reference member default initializer in C++?

From Dev

what is lifetime of static dictionary | LifestyleTransient | C# | WCF

Related Related

  1. 1

    Calling a Java variadic function from C through the JNI

  2. 2

    JNI calling Java from C++ with multiple threads

  3. 3

    JNI: Calling a java method from C periodically is not working

  4. 4

    Java JNI NullPointerException after calling method from C with valid pointers

  5. 5

    Calling RenderScript from C / JNI

  6. 6

    What's "Method Signature" parameter when calling a Java method using JNI?

  7. 7

    Calling a static void Java method from JNI

  8. 8

    JNI Calling Java Method With Array Parameter

  9. 9

    Calling JNI C function in another source file

  10. 10

    Calling dll implemented JNI from C++

  11. 11

    Calling C system calls from JNI

  12. 12

    JNI C++ UnsatisfiedLinkError When Calling Method

  13. 13

    Calling java function from c++ using jni: Failed to find static method id

  14. 14

    Error while calling C# from java using jni4net from 64 bit Os

  15. 15

    Build a dll in C++ for java which is calling another dll (JNI on Eclipse)

  16. 16

    What is the lifetime of unassigned object created in a constructor in java?

  17. 17

    What is the lifetime of an ejb client java ee 7

  18. 18

    JNI: From C code to Java and JNI

  19. 19

    What is the lifetime of a static variable in a C++ function?

  20. 20

    JNI and Java: ant calling make or make calling ant?

  21. 21

    Java C++ without JNI

  22. 22

    What happens if I call a java function from multiple threads from C with JNI?

  23. 23

    JNI, Garbage collection and Pointers- Java/C++ who should do what?

  24. 24

    Calling Java Methods from JNI results in program crash

  25. 25

    Calling into a saved java object via JNI from a different thread

  26. 26

    Calling a Java Method from the native code using jni

  27. 27

    JNI vs JNA, calling Java from Fortran95

  28. 28

    What is the lifetime of reference member default initializer in C++?

  29. 29

    what is lifetime of static dictionary | LifestyleTransient | C# | WCF

HotTag

Archive