gpt4 book ai didi

java - JVM 在 attachCurrentThread JNI 回调调用时崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 04:40:56 24 4
gpt4 key购买 nike

希望你一切顺利。

我在 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

最佳答案

我想您的 Thread 已经可以附加到 VM,因此不需要 AttachCurrentThread。也许您只需要像这样检查线程状态:

How to obtain JNI interface pointer (JNIEnv *) for asynchronous calls

关于java - JVM 在 attachCurrentThread JNI 回调调用时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26677202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com