简体   繁体   English

JNI,从C ++调用Java,在CallObjectMethod上使用SIGSEGV

[英]JNI, calling Java from C++, SIGSEGV on CallObjectMethod

I have been stuck for a few days on trying to solve problems of calling Java from C++ using JNI. 我已经停留了几天试图解决使用JNI从C ++调用Java的问题。

This is my C++ code. 这是我的C ++代码。 I call a java function which calls C++, and in that C++ function I am trying to call another Java function using the JNIEnv I received. 我调用了一个Java函数,该函数调用了C ++,在该C ++函数中,我试图使用收到的JNIEnv调用另一个Java函数。 The error seems to originate at the CallObjectMethod. 该错误似乎起源于CallObjectMethod。

JNIEXPORT jbyteArray JNICALL Java_Core_passJavaEnvToCpp(JNIEnv *env, jobject object) {

    jclass myClass = env->FindClass("Core");

    if (env->ExceptionCheck()) {
        env->ExceptionDescribe();
        env->ExceptionClear();
    }
    std::cout << "# 1" << std::endl;
    jmethodID mID = env->GetMethodID(myClass, "retrieveData", "(Ljava/lang/String;)[B");

    if (env->ExceptionCheck()) {
        env->ExceptionDescribe();
        env->ExceptionClear();
    }

    std::cout << "# 2" << std::endl;
    jbyteArray data = (jbyteArray) env->CallObjectMethod(myClass, mID, (jstring)"<location of data>");

    if (env->ExceptionCheck()) {
        env->ExceptionDescribe();
        env->ExceptionClear();
    }
    std::cout << "# 3" << std::endl;

    return data;

}

This is the error I get: 这是我得到的错误:

# 1
# 2
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x000000010ba5dbe6, pid=8193, tid=13063
#
# JRE version: Java(TM) SE Runtime Environment (8.0_25-b17) (build 1.8.0_25-b17)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.25-b02 mixed mode bsd-amd64 compressed oops)
# Problematic frame:
# V  [libjvm.dylib+0x30abe6]  Fingerprinter::Fingerprinter(methodHandle)+0x1a
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /Users/.../Downloads/apache-tomcat-8.0.21/bin/hs_err_pid8193.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.sun.com/bugreport/crash.jsp
#

This is part of the log report: 这是日志报告的一部分:

    Stack: [0x0000000125c20000,0x0000000125d20000],  sp=0x0000000125d1e640,  free space=1017k
 92 Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
 93 V  [libjvm.dylib+0x30abe6]  Fingerprinter::Fingerprinter(methodHandle)+0x1a
 94 V  [libjvm.dylib+0x30987a]  jni_invoke_nonstatic(JNIEnv_*, JavaValue*, _jobject*, JNICallType, _jmethodID*, JNI_ArgumentPusher*, Thread*)+0x244
 95 V  [libjvm.dylib+0x2f9fa3]  jni_CallObjectMethodV+0xf8
 96 V  [libjvm.dylib+0x30d15a]  checked_jni_CallObjectMethodV+0x113
 97 C  [transcoder.dylib+0x10a9]  JNIEnv_::CallObjectMethod(_jobject*, _jmethodID*, ...)+0x189
 98 C  [transcoder.dylib+0xc3e]  Java_Core_passJavaEnvToCpp+0x10e
 99 j  Core.passJavaEnvToCpp()[B+0

Anyone came upon anything similar? 有人遇到类似的事情吗? I have read about several different possibilities from hardware RAM issues, through JVM version issues and I don't know how to proceed any further. 从硬件RAM问题到JVM版本问题,我已经读到了几种不同的可能性,而且我不知道如何进一步进行。 I will be greatful for any advice. 我将不胜感激任何建议。

FIXED: I should have properly checked the function parameters when I got this process from here. 修正:从这里获得此过程时,我应该已经正确检查了功能参数。

You have to pass jobject instead of myClass in CallObjectMethod(). 您必须在CallObjectMethod()中传递jobject而不是myClass。 Bcoz that method is not a static method to take class object. Bcoz认为该方法不是采取类对象的静态方法。 Its an instance method so pass object of that class. 它是一个实例方法,因此可以传递该类的对象。 – Saritha G – Saritha G

Convert like this: 像这样转换:

 char *data= (char*)malloc(16);
 strcpy(data, "location of data");
 jstring jstrBuf = env->NewStringUTF(env, data);

Create new instance for your class and pass that object in CallObjectMethod(), instead of passing myClass. 为您的类创建新实例,然后在CallObjectMethod()中传递该对象,而不是传递myClass。

C-style casting C型铸造

(jstring)"<location of data>"

looks very suspicious. 看起来很可疑。 You are trying to cast array of chars into Java String object. 您正在尝试将字符数组转换为Java String对象。 And casting result into (jbyteArray) is probably wrong as well. 并且将结果转换为(jbyteArray)也可能是错误的。 Basically neither C-style or C++-style casting should be used here. 基本上,这里不应该使用C风格或C ++风格的转换。 Construct jstring and other objects properly. 正确构造jstring和其他对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM