JNI calling Java from C++ with multiple threads

nabroyan

I'm working on a project, where I call Java functions from C++ code (using JNI) and I have a problem about multithreading. I want to call Java searching function and for each call I want to make a separate thread. I have a singleton MainClass and nested Query class. Query class is inherited from QThread. Code looks like this

MainClass::MyQuery query("<some search query>");
query.LaunchQuery();


//functions of Query   
void MainClass::MyQuery::LaunchQuery() const
{
    this->start();
}

void MainClass::Query::run()
{
    const MainClass& mainClass = MainClass::GetInstance();
    const jclass& obj = mainClass.GetClass();
    JNIEnv& env = mainClass.GetJavaEnvironment();
    jmethodID methodId = env.GetMethodID(obj, "SearchQuery", "(Ljava/lang/String;)V"); //Run-time error

    if(methodId != 0)
    {
        //calling "SearchQuery" function
    }

Now, if run this code in a single thread - everything is fine, but if try to run above code - using multithreading, it causes run-time error by message "Unhandled exception at 0x777715de in MyApp.exe: 0xC0000005: Access violation reading location 0x000000ac." when I try to get method id. I've tried also with boost::thread but result was the same.

So why it fails when I'm doing it in a separate thread, when in the same thread everything is fine? Any ideas?

Graham Griffiths

Scroll down to 'Attaching to the VM' in the JNI docs :

http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/invocation.html

The JNI interface pointer (JNIEnv) is valid only in the current thread. Should another thread need to access the Java VM, it must first call AttachCurrentThread() to attach itself to the VM and obtain a JNI interface pointer.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling accept() from multiple threads

From Java

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

From Java

Best practices for Java logging from multiple threads?

From Java

passing string array from java to C with JNI

From Java

Write to FileOutputStream from multiple threads in Java

From Java

JNI: From C code to Java and JNI

From Java

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

From Java

JNI: passing integer array from Java to C

From Dev

Is calling wait() on a std::future multiple times and from multiple threads safe?

From Dev

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

From Dev

Calling a static void Java method from JNI

From Dev

Is calling std::copy from multiple threads for different ranges of the same vector safe?

From Dev

Java JNI NullPointerException after calling method from C with valid pointers

From Dev

Calling methods from different threads

From Dev

Calling dll implemented JNI from C++

From Dev

Calling vector of threads from destructor

From Dev

Calling C system calls from JNI

From Dev

Pass multiple parameters from java to c++ application via JNI

From Dev

Calling a Java Method from the native code using jni

From Dev

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

From Dev

JNI vs JNA, calling Java from Fortran95

From Dev

Java threads calling parent functions

From Dev

Linux, C: Accumulate data from multiple threads

From Dev

Java, Calling an AlertDialog from multiple classes

From Dev

Calling Java Methods from JNI results in program crash

From Dev

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

From Dev

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

From Dev

JNI conversion from Java to C++

From Dev

Calling RenderScript from C / JNI

Related Related

  1. 1

    Calling accept() from multiple threads

  2. 2

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

  3. 3

    Best practices for Java logging from multiple threads?

  4. 4

    passing string array from java to C with JNI

  5. 5

    Write to FileOutputStream from multiple threads in Java

  6. 6

    JNI: From C code to Java and JNI

  7. 7

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

  8. 8

    JNI: passing integer array from Java to C

  9. 9

    Is calling wait() on a std::future multiple times and from multiple threads safe?

  10. 10

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

  11. 11

    Calling a static void Java method from JNI

  12. 12

    Is calling std::copy from multiple threads for different ranges of the same vector safe?

  13. 13

    Java JNI NullPointerException after calling method from C with valid pointers

  14. 14

    Calling methods from different threads

  15. 15

    Calling dll implemented JNI from C++

  16. 16

    Calling vector of threads from destructor

  17. 17

    Calling C system calls from JNI

  18. 18

    Pass multiple parameters from java to c++ application via JNI

  19. 19

    Calling a Java Method from the native code using jni

  20. 20

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

  21. 21

    JNI vs JNA, calling Java from Fortran95

  22. 22

    Java threads calling parent functions

  23. 23

    Linux, C: Accumulate data from multiple threads

  24. 24

    Java, Calling an AlertDialog from multiple classes

  25. 25

    Calling Java Methods from JNI results in program crash

  26. 26

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

  27. 27

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

  28. 28

    JNI conversion from Java to C++

  29. 29

    Calling RenderScript from C / JNI

HotTag

Archive