2

I have created a JNI wrapper with swig for a library. I use swig directors to call back into the JVM. Some of these callbacks occur on threads created within the native library. The first callback on a non JVM thread fails with a SIGSEGV which I could trace down to ignoring the negative return value (-1) of AttachCurrentThread and thus dereferencing a jenv pointer which is actually null. This happens in the swig generated code.

I have tried to call back into the JVM via the director classes from a thread that I created on the native side. This works fine. It does however not work from the thread created by the library I wrapped.

What can be possible reasons that AttachCurrentThread fails?

user207421
  • 305,947
  • 44
  • 307
  • 483
sun
  • 97
  • 8
  • Sounds like this is more a generic JNI question than a SWIG one. See http://stackoverflow.com/questions/12900695/how-to-obtain-jni-interface-pointer-jnienv-for-asynchronous-calls. Meanwhile can you make a minimal, complete example that illustrates this perhaps? That would make figuring out a solution much simpler for someone. – Flexo Apr 08 '16 at 07:53

1 Answers1

2

When a thread does not have sufficient stack space left AttachCurrentThread fails. Unfortunately the documentation of the Oracle JVM does not mention the minimum required stack space for AttachCurrentThread to work.

In my case the native library was optimized for embedded hardware and therefore creates threads generally with a stack size of 100000 bytes.

sun
  • 97
  • 8