Fix crashes in swig_connect_director during director class construction.

Occurs when using the director class from multiple threads - a race condition
initialising block scope static variables.

Block scope static variables are guaranteed to be thread safe in C++11,
so the fix is guaranteed when using C++11. However, most modern compilers
also fix it when using C++03/C++98.

Closes #1862
This commit is contained in:
William S Fulton 2020-08-28 18:23:47 +01:00
commit b018c32f9d
3 changed files with 30 additions and 21 deletions

View file

@ -215,6 +215,15 @@ namespace Swig {
}
};
struct SwigDirectorMethod {
const char *name;
const char *desc;
jmethodID methid;
SwigDirectorMethod(JNIEnv *jenv, jclass baseclass, const char *name, const char *desc) : name(name), desc(desc) {
methid = jenv->GetMethodID(baseclass, name, desc);
}
};
/* Java object wrapper */
JObjectWrapper swig_self_;
@ -238,6 +247,11 @@ namespace Swig {
}
}
jclass swig_new_global_ref(JNIEnv *jenv, const char *classname) {
jclass clz = jenv->FindClass(classname);
return clz ? (jclass)jenv->NewGlobalRef(clz) : 0;
}
public:
Director(JNIEnv *jenv) : swig_jvm_((JavaVM *) NULL), swig_self_() {
/* Acquire the Java VM pointer */