2014-10-31 124 views
1

希望你做得很好。JVM崩潰attachCurrentThread JNI回調調用

我在C中有一個JNI實現,其中一個C函數附加到當前的JVM線程並回調一個Java方法,這實際上是在attachCurrentThread()函數調用時崩潰JVM。 我的實現基於鏈接中給出的答案Keeping a global reference to the JNIEnv environment

線程創建部分將偵聽套接字連接以接受事件並注入GetEvent函數。

// create thread for reading from socket. 
     pthread_attr_init(&plafParams->readThreadAttr); 
     pthread_attr_setdetachstate(&plafParams->readThreadAttr, PTHREAD_CREATE_JOINABLE); 
     pthread_attr_setstacksize(&plafParams->readThreadAttr, 16384); 
     plafParams->eventThreadFlag = 2; 
     if(pthread_create(&plafParams->hReadThread_p, &plafParams->readThreadAttr, (void *) plafIncomingPortReadThread, (void *) dlmsInstance)) 
     { 

      plafFree(plafParams); 
      return FAILURE; 
     } 

     //create thread to read from platform (to monitor wrapperRecv() process) 
     pthread_attr_init(&plafParams->EventthreadAttr); 
     pthread_attr_setdetachstate(&plafParams->EventthreadAttr, PTHREAD_CREATE_JOINABLE); 
     pthread_attr_setstacksize(&plafParams->EventthreadAttr, 16384); 

     if(pthread_create(&plafParams->EventThreadHandle_p, &plafParams->EventthreadAttr, (void *) IncomingPortEventReadHandling, (void *) dlmsInstance)) 
     { 
      pthread_attr_destroy(&plafParams->readThreadAttr); 
      plafParams->eventThreadFlag = 0; 
      return FAILURE; 
     } 



static JavaVM *jvm; 

// Caching the JVM on JVM OnLoad 
jint JNI_OnLoad(JavaVM* vm, void* reserved) 
{ 
    JNIEnv* jenv; 
    version = (*vm)->GetEnv(vm, (void **) &jenv, JNI_VERSION_1_6); 
    jvm = vm; 
    printf("\n jvm = %d \n",rs); 
    if(rs == JNI_OK) { 
     printf("\n JNI_OK \n"); 
    } 
} 

回調函數

void GetEvent(unsigned char *msg) 
    { 

     printf("\n Processing event data \n"); 
     JNIEnv *env; 

     printf("\n Before attaching JVM thread \n"); 
     jint rs = (*jvm)->AttachCurrentThread(jvm, &env, NULL); // JVM crashes here, tried with jint rs = (*jvm)->AttachCurrentThread(jvm, (void **) &env, NULL), nothing helps 
     printf("\n After attaching JVM thread \n"); 

    } 

在GetEvent的執行()在接收到的事件時,我的AttachCurrentThread JVM崩潰()函數調用。 有人能幫我找出問題嗎? 在此先感謝和任何建議將不勝感激。

問候,

Saravanan摹

+0

更多信息需要有搞清楚什麼地方出了錯的希望。在附註中,'jint rs =(* jenv) - > GetJavaVM(jenv,&jvm);''不需要,您可以使用爲您提供的'JavaVM * vm'指針onload。EX:'jvm = vm;' – 2014-10-31 16:58:02

+0

謝謝亞歷克斯。我已經刪除了用jvm = vm替換的GetJavaVM()函數 – srvnn 2014-11-01 09:36:52

回答