这是我的第一篇文章,所以请大家理解一下。 我有一些java代码,我有一些本机代码。处理本地代码中的信号 - 在终端中使用SIGSEGV导致JVM崩溃 -
的Java的部分是不是现在这么有趣,所以我会跳到C++的部分:
//some more trivial includes
#include <signal.h>
//these are global variables
jclass GLOBAL_CLASS;
JNIEnv * GLOBAL_ENV;
jobject GLOBAL_OBJECT;
jmethodID METHOD_ID;
void sigproc(int signo)
{
if (signo == SIGINT)
{
signal(SIGINT, sigproc);
//if Ctrl-c is pressed I want to call a method within my java class
//since I can pass only int to this function
//I've decided to use global variables
GLOBAL_ENV->CallVoidMethod(GLOBAL_OBJECT, METHOD_ID);
exit(0);
}
}
JNIEXPORT void JNICALL Java_intern_Work_readFromFile
(JNIEnv *env, jobject obj, jobjectArray arr)
{
/*define a signal trap! */
signal(SIGINT, sigproc);
//sigproc(SIGINT);
/*initialize the global variables */
GLOBAL_ENV = env;
GLOBAL_OBJECT = obj;
GLOBAL_CLASS = env->GetObjectClass(obj);
//method id is the same so it's better to cache it
//at the beginning
jmethodID mid = env->GetMethodID(GLOBAL_CLASS,
"nativeListener",
"(Ljava/lang/String;)V");
METHOD_ID = GLOBAL_ENV->GetMethodID(GLOBAL_CLASS,
"closeEverything", "()V");
//let's say I have a while(true) block just below
//and some more work is done.
}
此功能被触发我MainClass的开始。 该程序运行正常,如果我删除
GLOBAL_ENV->CallVoidMethod(GLOBAL_OBJECT, METHOD_ID);
但问题是,我需要它,因为我打算释放一些动态分配的内存+我需要调用我的这个类的功能。 (换句话说,当我在终端中按ctrl-c时,它表示JVM使用SIGSEGV)
看起来我并没有真正理解从内核传递信号时到底发生了什么。我的全局变量GLOBAL_ENV仍然是我可以使用的正确指针吗?
谁能告诉我一个优雅的方式来解决我的问题? 或者任何的指导也欢迎! 任何解释......任何事情。 在此先感谢!
这里的JVM崩溃代码示例:
A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007f9974cfc021, pid=7099, tid=140297087112960
#
# JRE version: 6.0_24-b24
# Java VM: OpenJDK 64-Bit Server VM (20.0-b12 mixed mode linux-amd64 compressed oops)
# Derivative: IcedTea6 1.11.4
# Distribution: Ubuntu 12.04 LTS, package 6b24-1.11.4-1ubuntu0.12.04.1
# Problematic frame:
# V [libjvm.so+0x617021] methodOopDesc::result_type() const+0x31
正在使用JNA的一个选项?我认为它更简单,更像Python的ctypes。 – dstromberg