0

I need to create an instance of a Java class in my native code. To do it, I am using the following C code:

jobject Java_com_mypackage__myClass_myMethod(JNIEnv* env, jobject thiz, jint index){
    int fd = pDevs[index].ufds.fd; // fd = open(....); it's a input/eventX file.
    jclass class = (*env)->FindClass(env,"com/mypackage/ClassName");
    jmethodID mid = (*env)->GetMethodID(env,class,"<init>","(Ljava/lang/String;)V");
    return (*env)->NewObject(env,class,mid,(*env)->NewStringUTF(env, pDevs[index].device_path));
}

But when I invoke myMethod, I keep getting fatal signal 11 (SIGSEGV). Is the code wrong?

1
  • 2
    Your system log probably contains important information, e.g. in which function the signal was issued. Also, you should check the return values fron JNI calls you make: isn't class NULL? Isn't mid NULL? Commented Jul 13, 2013 at 11:02

1 Answer 1

1

You should use logging/debbuger to find place where segmentation fault happenned. The easiest way is to use android logging system as described here

jclass class = (*env)->FindClass(env,"com/mypackage/ClassName");
if(class == null)
{
  __android_log_print(ANDROID_LOG_VERBOSE, "TAG", "class is null");
}

For example if ClassName is an inner class of some activity you should use com/mypackage/ActivityName#ClassName instead of com/mypackage/ClassName. But I can only guess before you provide your logs.

Sign up to request clarification or add additional context in comments.

1 Comment

I wasn't aware of how to print logs in native code, this was really helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.