title: JVM crashing on attachCurrentThread JNI callback invocation
body: <p>Hope you are doing well.</p>
  
  <p>I have a JNI implementation in C wherein a C function attach to current JVM thread and callback a Java method, which is actually crashing the JVM upon attachCurrentThread() function call.
  My implementation is based on the answer given in the link <a href="https://stackoverflow.com/questions/12420463/jni-keeping-global-reference-to-the-environment">Keeping a global reference to the JNIEnv environment</a></p>
  
  <p>Thread creation part which will listen for a socket connection to accept an event and inject the GetEvent function.</p>
  
  <pre><code>// create thread for reading from socket.
          pthread_attr_init(&amp;plafParams-&gt;readThreadAttr);
          pthread_attr_setdetachstate(&amp;plafParams-&gt;readThreadAttr, PTHREAD_CREATE_JOINABLE);
          pthread_attr_setstacksize(&amp;plafParams-&gt;readThreadAttr, 16384);
          plafParams-&gt;eventThreadFlag = 2;
          if(pthread_create(&amp;plafParams-&gt;hReadThread_p, &amp;plafParams-&gt;readThreadAttr, ( void * ) plafIncomingPortReadThread, ( void * ) dlmsInstance))
          {
  
              plafFree(plafParams);
              return FAILURE;
          }
  
          //create thread to read from platform (to monitor wrapperRecv() process)
          pthread_attr_init(&amp;plafParams-&gt;EventthreadAttr);
          pthread_attr_setdetachstate(&amp;plafParams-&gt;EventthreadAttr, PTHREAD_CREATE_JOINABLE);
          pthread_attr_setstacksize(&amp;plafParams-&gt;EventthreadAttr, 16384);
  
          if( pthread_create(&amp;plafParams-&gt;EventThreadHandle_p, &amp;plafParams-&gt;EventthreadAttr, ( void * ) IncomingPortEventReadHandling, ( void * ) dlmsInstance))
          {
              pthread_attr_destroy(&amp;plafParams-&gt;readThreadAttr);
              plafParams-&gt;eventThreadFlag = 0;
              return FAILURE;
          }
  
  
  
  static JavaVM *jvm;
  
  // Caching the JVM on JVM OnLoad
  jint JNI_OnLoad(JavaVM* vm, void* reserved) 
  {
      JNIEnv* jenv;
      version = (*vm)-&gt;GetEnv(vm, (void **) &amp;jenv, JNI_VERSION_1_6);
      jvm = vm;
      printf("\n jvm = %d \n",rs);
      if(rs == JNI_OK) {
          printf("\n JNI_OK \n");
      }
  }
  </code></pre>
  
  <p>Callback function</p>
  
  <pre><code>void GetEvent(unsigned char *msg)
      {
  
          printf("\n Processing event data \n");
          JNIEnv *env;
  
          printf("\n Before attaching JVM thread \n");
          jint rs = (*jvm)-&gt;AttachCurrentThread(jvm, &amp;env, NULL); // JVM crashes here, tried with jint rs = (*jvm)-&gt;AttachCurrentThread(jvm, (void **) &amp;env, NULL), nothing helps
          printf("\n After attaching JVM thread \n");
  
      }
  </code></pre>
  
  <p>Upon execution of GetEvent() on receiving a event, my JVM crashes on AttachCurrentThread() function call.
  Could someone help me to identify the problem? 
  Thanks in advance and any suggestions would be greatly appreciated.</p>
  
  <p>regards,</p>
  
  <p>Saravanan G</p>