mp/mp-jvm-destructor.patch

65 lines
1.9 KiB
Diff

--- mp-7fd4828c934fccf7367499c9e01cc9a1e90a2093/solvers/jacop/java.cc.orig 2020-03-03 23:39:58.000000000 -0700
+++ mp-7fd4828c934fccf7367499c9e01cc9a1e90a2093/solvers/jacop/java.cc 2021-01-15 14:48:04.231938853 -0700
@@ -119,7 +119,7 @@ std::string RegKey::GetStrValue(fmt::CSt
namespace mp {
-JVM JVM::instance_;
+JVM *JVM::instance_;
void Env::Throw(jthrowable exception, const char *method_name) {
env_->ExceptionClear();
@@ -190,8 +190,16 @@ JVM::~JVM() {
jvm_->DestroyJavaVM();
}
+void JVM::cleanup_jvm()
+{
+ if (instance_) {
+ delete instance_;
+ instance_ = nullptr;
+ }
+}
+
Env JVM::env(const char *const *options) {
- if (!instance_.jvm_) {
+ if (!instance_) {
#ifdef _WIN32
std::string runtime_lib_path;
bool exists = false;
@@ -251,15 +259,19 @@ Env JVM::env(const char *const *options)
}
vm_args.nOptions = static_cast<jint>(jvm_options.size());
vm_args.options = &jvm_options[0];
+ instance_ = new JVM();
void *envp = 0;
- jint result = JNI_CreateJavaVM(&instance_.jvm_, &envp, &vm_args);
+ jint result = JNI_CreateJavaVM(&instance_->jvm_, &envp, &vm_args);
if (result != JNI_OK) {
+ delete instance_;
+ instance_ = nullptr;
throw JavaError(fmt::format(
"Java VM initialization failed, error code = {}", result));
}
- instance_.env_ = Env(static_cast<JNIEnv*>(envp));
+ instance_->env_ = Env(static_cast<JNIEnv*>(envp));
+ std::atexit(cleanup_jvm);
}
- return instance_.env_;
+ return instance_->env_;
}
ClassBase::~ClassBase() {}
--- mp-7fd4828c934fccf7367499c9e01cc9a1e90a2093/solvers/jacop/java.h.orig 2020-03-03 23:39:58.000000000 -0700
+++ mp-7fd4828c934fccf7367499c9e01cc9a1e90a2093/solvers/jacop/java.h 2021-01-15 14:33:39.263164981 -0700
@@ -215,7 +215,8 @@ class JVM {
private:
JavaVM *jvm_;
Env env_;
- static JVM instance_;
+ static JVM *instance_;
+ static void cleanup_jvm();
JVM() : jvm_() {}
~JVM();