使用dlopen在Mac上加载libjvm.dylib时发出信号SIGSEGV

时间:2018-08-09 01:17:10

标签: c++ xcode macos java-native-interface

我正在尝试从Mac中的libjvm.dylib获取正确指向JNI_CReateJVM的函数指针。 Windows中的对应项正在工作。因此,指向libjvm.dylib(m_jvm_dll)的指针不是null,即库的路径正确。同样,m_createJVM_Ptr不为null。因此,我肯定会得到指示。但是,一旦我使用m_createJVM_Ptr,就会在Xcode中显示以下Thread1:signal SIGSEGV错误:

->  0x11566c2b4: movl   (%rsi), %eax
0x11566c2b6: leaq   0xf8(%rbp), %rsi
0x11566c2bd: vmovdqu %ymm0, (%rsi)
0x11566c2c1: vmovdqu %ymm7, 0x20(%rsi)
0x11566c2c6: vmovdqu %ymm8, 0x40(%rsi)
0x11566c2cb: vmovdqu %ymm15, 0x60(%rsi)

这是罪魁祸首。

bool JVMInitializer::create_JVM(SettingDlg* inp_settingDlg){
wxString jvm_path;
wxString ij_path;
wxString bioformats_path;
//if (inp_settingDlg)
//{
//  jvm_path = inp_settingDlg->getJVMPath();
//  ij_path = inp_settingDlg->getIJPath();
//  bioformats_path = inp_settingDlg->getBioformatsPath();
//}

//Loading JVM library and methods.
jvm_path = "/Users/dev/Downloads/ImageJ/jre/lib/server/libjvm.dylib";
#ifdef _WIN32
m_jvm_dll = LoadLibraryW(jvm_path.ToStdWstring().c_str());
#else
m_jvm_dll = dlopen((const char*)jvm_path.mb_str(wxConvUTF8), RTLD_NOW);
#endif
if (m_jvm_dll == nullptr)
    return false;

#ifdef _WIN32
m_createJVM_Ptr = (decltype(&JNI_CreateJavaVM))GetProcAddress(m_jvm_dll, "JNI_CreateJavaVM");
#else
m_createJVM_Ptr = (CreateJavaVM_t*) dlsym(m_jvm_dll, "JNI_CreateJavaVM");
#endif
if (m_createJVM_Ptr == nullptr)
  return false;

using namespace std;    
JavaVMOption* options = new JavaVMOption[1];
//Geting absolute path to class file.
wxString exePath = wxStandardPaths::Get().GetExecutablePath();
exePath = wxPathOnly(exePath);
string imageJPath = "-Djava.class.path=" + exePath + GETSLASH() + "Java_Code" + GETSLASH() + getPathSeparator();
//imageJPath.append(ij_path + getPathSeparator());
//imageJPath.append(bioformats_path);

imageJPath.append(exePath + GETSLASH() + "Java_Code" + GETSLASH() + "ij.jar;");
imageJPath.append(exePath + GETSLASH() + "Java_Code" + GETSLASH() + "SlideBook6Reader.jar;");
imageJPath.append(exePath + GETSLASH() + "Java_Code" + GETSLASH() + "bioformats_package.jar;");

options[0].optionString = const_cast<char*>(imageJPath.c_str());

m_VMargs.version = JNI_VERSION_1_6;             // minimum Java version
m_VMargs.nOptions = 1;                          // number of options
m_VMargs.options = options;
m_VMargs.ignoreUnrecognized = false;     // invalid options make the JVM init fail

jint rc = m_createJVM_Ptr(&m_pJvm, (void**)&m_pEnv, &m_VMargs);
delete[] options;
if (rc != JNI_OK) {
    return false;
}
return true;
}

故障发生在jint rc = .....(最后7行)。

以下是源文件的链接: jvminitializer.cpp jvminitializer.h

因此,我在运行代码时没有动态加载jvm库,并且可以通过直接调用JNI_CreateJavaVM方法来工作,但我希望它可以动态加载。还要注意的一点是,我正在imageJ内使用JRE进行Java 1.8附带的动态加载。认为此信息可能相关。

0 个答案:

没有答案