EJane EJane - 2 years ago 145
Java Question

JNI segfaults creating new object

I'm new to JNI (and java), so I apologize in advance if this is just a stupid mistake. But after much searching I can't find an explanation or a solution.

I have a parametric Java class called

. The Java constructor for
takes an object
and a long
. The C code has a value and is supposed to create a Tagged object with the value
and the memory address of that value. However, I get a segfault when I call NewObject. Not sure if the problem is the generic Type constructor (called with an integer), a mismatch between Java/C integer types (long vs long long vs long), a stupid mistake, or something I haven't considered.

Java Class:

public class Tagged<T> {
private final T value;
private long ptr;

private TaggedValue(T value, long ptr){
this.value = value;
this.ptr = ptr;

JNI code:

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");
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: %i\n",val);
long long addr = (long long) valptr;
printf("Address: %p = %lld = %p\n",valptr,addr,(void *)addr);

// Try to create Tagged object
jobject taggedval = (*env)->NewObject(env, c, constructor, val, addr);
printf("We never get here\n");

return taggedval;

Console output:

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

Thanks for any/all help!

Answer Source

Your constructor takes an jobject and a long, and you are passing it an int and a long long.

Maybe you probably meant to wrap the int into a java Integer? And you should probably cast the long to a jlong too, just in case long long and jlong arent the same type.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download