创建新对象的 JNI 段错误



我是JNI(和java)的新手,所以如果这只是一个愚蠢的错误,我提前道歉。但是经过多次搜索,我找不到解释或解决方案。

我有一个名为 Tagged<T> 的参数化 Java 类。Tagged<T>的 Java 构造函数采用对象T和长ptr。C 代码有一个值,应该创建一个具有值 v 和该值的内存地址的标记对象。但是,当我调用 NewObject 时,我得到一个段错误。不确定问题是通用类型构造函数(用整数调用)、Java/C 整数类型之间的不匹配(长与长)、愚蠢的错误还是我没有考虑过的事情。

爪哇类:

public class Tagged<T> {
    private final T value;
    private long ptr;
    private TaggedValue(T value, long ptr){
        this.value = value;
        this.ptr = ptr;
    }
}

JNI 代码:

JNIEXPORT jobject JNICALL Java_package_Class_function (JNIEnv * env, jclass cls, ...){
  // Find Java class
  jclass c = (*env)->FindClass(env, "package/Tagged");
  if (c == 0) {
      printf("Find Class Failed.n");
  }else{
      printf("Found class.n");
  }
  // Find Tagged<T> constructor
  jmethodID constructor = (*env)->GetMethodID(env,c, "<init>", "(Ljava/lang/Object;J)V");
  if (constructor == 0) {
      printf("Find method Failed.n");
  } else {
      printf("Found method.n");
  }
  // Get value 
  int * valptr = LibraryCall();
  // check that constructor arguments are what we expect
  int val = (int) *valptr;
  printf("Value:  %in",val);
  long long addr = (long long) valptr;
  printf("Address: %p = %lld = %pn",valptr,addr,(void *)addr);
  // Try to create Tagged object
  jobject taggedval = (*env)->NewObject(env, c, constructor, val, addr);
  printf("We never get heren");
  return taggedval;
}

控制台输出:

Found class.
Found method.
Value:  102583
Address: 0x7fdcc2d209b0 = 140586138077616 = 0x7fdcc2d209b0
#
# A fatal error has been detected by the Java Runtime Environment: 
#
#  SIGSEGV (0xb) at pc=0x0000000109ae9bcf, pid=42140, tid=3847
#
# JRE version: Java(TM) SE Runtime Environment (8.0_66-b17) (build 1.8.0_66-b17)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.66-b17 mixed mode bsd-amd64 compressed oops)
# Problematic frame:
# V  [libjvm.dylib+0x2e9bcf]  JavaCallArguments::parameters()+0x27
#
# 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/eleanor/Documents/workspace/av-java/src/hs_err_pid42140.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
#
Abort trap: 6

感谢您的任何/所有帮助!

你的构造函数采用jobjectlong,并且你正在传递它一个int和一个long long

也许您可能打算将int包装到 java Integer中?您可能也应该将长投到jlong,以防万一long longjlong不是同一类型。

相关内容

  • 没有找到相关文章

最新更新