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

Drake Amara :

I have a java object which calls into a C++ shared object via JNI. In C++, I am saving a reference to the JNIEnv and jObject.

JavaVM * jvm;
JNIEnv * myEnv;
jobject myobj;

JNIEXPORT void JNICALL Java_org_api_init
   (JNIEnv *env, jobject jObj) {
    myEnv = env;
    myobj = jObj;
}

I also have a GLSurface renderer and it eventually calls the C++ shared object mentioned above on a different thread, the GLThread. I am then trying to call back into my original Java object using the jobject I saved initially, but I think because I am on the GLThread, I get the following error.

W/dalvikvm(16101): JNI WARNING: 0x41ded218 is not a valid JNI reference
I/dalvikvm(16101): "GLThread 981" prio=5 tid=15 RUNNABLE
I/dalvikvm(16101):   | group="main" sCount=0 dsCount=0 obj=0x41d6e220 self=0x5cb11078
I/dalvikvm(16101):   | sysTid=16133 nice=0 sched=0/0 cgrp=apps handle=1555429136
I/dalvikvm(16101):   | schedstat=( 0 0 0 ) utm=42 stm=32 core=1

The code calling back into Java :

void setData()
    {
        jvm->AttachCurrentThread(&myEnv, 0);

        jclass javaClass = myEnv->FindClass("com/myapp/myClass");
        if(javaClass == NULL){
            LOGD("ERROR - cant find class");
        }

        jmethodID method = myEnv->GetMethodID(javaClass, "updateDataModel", "()V");
        if(method == NULL){
            LOGD("ERROR - cant access method");
        }

        // this works, but its a new java object
        //jobject myobj2 = myEnv->NewObject(javaClass, method);

        //this is where the crash occurs
        myEnv->CallVoidMethod(myobj, method, NULL); 

}

If instead I create a new jObject using env->NewObject, I can succuessfully call back into Java, but it is a new object and I dont want that. I need to get back to my original Java Object.

Is it a matter of switching threads before I call back into Java? If so, how do I do so ?

Mārtiņš Možeiko :

Accessing object from different threads is fine. Problem is that JNI calls get objects as local references. If you want to keep reference to jobject between JNI calls you need to make it global reference:

myobj = env->NewGlobalRef(jObj);

Remember to free it after you're done using it otherwise garbage collector won't collect it and you'll get memory leaks:

myEnv->DeleteGlobalRef(myobj);

Read about global vs local references here.

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 java method from different thread in ndk

From Java

Calling a DLL from an Applet via JNI

From Dev

"The calling thread cannot access this object because a different thread owns it" error when updating UI control from different thread in WPF

From Dev

Calling a static void Java method from JNI

From

The calling thread cannot access this object because a different thread owns it

From Dev

the calling thread cannot access this object because a different thread owns it wpf

From Dev

The Calling thread cannot access this object because a different thread owns it,WPF

From Dev

WPF: The calling thread cannot access this object because a different thread owns it

From Java

calling main thread from Runnable thread in java

From Dev

thread of different class object in java

From Dev

c# - Changes in cefsharp 79.1.35 (from 75.1.143) creates: 'The calling thread cannot access this object because a different thread owns it.'

From Dev

Famous the calling thread cannot access this object because a different issue

From Dev

Calling JMX connection from thread in Java?

From Java

Calling JNI function to create object

From Dev

Calling Java Methods from JNI results in program crash

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 a Java Method from the native code using jni

From Dev

JNI vs JNA, calling Java from Fortran95

From Dev

Calling java script function from different tags

From Dev

Calling RenderScript from C / JNI

From Dev

Is it thread safe to update different properties of an object from different threads?

From Java

Call static Java method from separate thread using JNI

From Java

Java ShutdownHook Unable to Join Main Thread when Run From JNI

From Java

How To Call JAVA Methods from inside of a Thread in JNI

From Dev

The calling thread cannot access this object because a different thread owns it, even when using the dispatcher

From Dev

ReactiveUI WPF - The calling thread cannot access this object because a different thread owns it

From Dev

The calling thread cannot access this object because a different thread owns it - BackgroundWorker error

Related Related

  1. 1

    Calling java method from different thread in ndk

  2. 2

    Calling a DLL from an Applet via JNI

  3. 3

    "The calling thread cannot access this object because a different thread owns it" error when updating UI control from different thread in WPF

  4. 4

    Calling a static void Java method from JNI

  5. 5

    The calling thread cannot access this object because a different thread owns it

  6. 6

    the calling thread cannot access this object because a different thread owns it wpf

  7. 7

    The Calling thread cannot access this object because a different thread owns it,WPF

  8. 8

    WPF: The calling thread cannot access this object because a different thread owns it

  9. 9

    calling main thread from Runnable thread in java

  10. 10

    thread of different class object in java

  11. 11

    c# - Changes in cefsharp 79.1.35 (from 75.1.143) creates: 'The calling thread cannot access this object because a different thread owns it.'

  12. 12

    Famous the calling thread cannot access this object because a different issue

  13. 13

    Calling JMX connection from thread in Java?

  14. 14

    Calling JNI function to create object

  15. 15

    Calling Java Methods from JNI results in program crash

  16. 16

    JNI calling Java from C++ with multiple threads

  17. 17

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

  18. 18

    Java JNI NullPointerException after calling method from C with valid pointers

  19. 19

    Calling a Java Method from the native code using jni

  20. 20

    JNI vs JNA, calling Java from Fortran95

  21. 21

    Calling java script function from different tags

  22. 22

    Calling RenderScript from C / JNI

  23. 23

    Is it thread safe to update different properties of an object from different threads?

  24. 24

    Call static Java method from separate thread using JNI

  25. 25

    Java ShutdownHook Unable to Join Main Thread when Run From JNI

  26. 26

    How To Call JAVA Methods from inside of a Thread in JNI

  27. 27

    The calling thread cannot access this object because a different thread owns it, even when using the dispatcher

  28. 28

    ReactiveUI WPF - The calling thread cannot access this object because a different thread owns it

  29. 29

    The calling thread cannot access this object because a different thread owns it - BackgroundWorker error

HotTag

Archive