希望你一切顺利。
我在 C 中有一个 JNI 实现,其中一个 C 函数附加到当前 JVM 线程并回调一个 Java 方法,这实际上在调用 attachCurrentThread() 函数时使 JVM 崩溃。我的实现是基于链接中给出的答案 Keeping a global reference to the JNIEnv environment
线程创建部分将监听套接字连接以接受事件并注入(inject) 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() 后,我的 JVM 在 AttachCurrentThread() 函数调用时崩溃。有人可以帮我找出问题所在吗?提前致谢,如有任何建议,我们将不胜感激。
问候,
萨拉瓦南 G
我是一名优秀的程序员,十分优秀!