这是我的第一篇文章,所以请表现出一些理解。我有一些 java 代码和一些本机代码。
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 cheshes 与 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