Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I receive error message when run JNI android app A/libc﹕ Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1), thread 17729

I got an error when i run android app that I use JNI functions and c++ code in it. When it run, I got below message:

Fatal signal 11 (SIGSEGV) at 0xe480001d (code=1), thread 5465

And finally here is my codes:

JNIEXPORT jstring JNICALL Java_ir_bassir_ndktest4_MainActivity_getName
(JNIEnv *env, jobject obj){

  jclass cls = (*env)->GetObjectClass(env, obj);
  jmethodID mid = (*env)->GetStaticMethodID(env, cls, "testJava", "([Ljava/lang/String)[Ljava/lang/String");

  jstring plainText = (*env)->NewStringUTF(env, "Hello from JNI ! Compiled with ABI  2222 ");
  jstring result = (*env)->CallStaticObjectMethod(env, cls, mid, plainText);

  return (*env)->NewStringUTF(env, plainText);
}

And on the Java side:

public class MainActivity extends ActionBarActivity {

    public native String getName();

    public static String testJava(String txt){
        Log.d("BP","call back to java method");
        String result = txt + "its added in JAVA";
        return result;
    }

    static{
        System.loadLibrary("HelloJNI");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String name = getName();

        Log.d("BP",name);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
like image 536
bassir pechaz Avatar asked Sep 28 '22 21:09

bassir pechaz


2 Answers

The JNI side of this code is C, not C++, and C's laxer handling of pointer punning is part of the problem. Your code breaks on these two lines:

jstring plainText = (*env)->NewStringUTF(env, "Hello from JNI ! Compiled with ABI  2222 ");
return (*env)->NewStringUTF(env, plainText);

because NewStringUTF has the signature

jstring NewStringUTF(JNIEnv *env, const char *bytes);

Which means that this call is broken:

//                        vvvvvvvvv--- plainText is not of the right type!
(*env)->NewStringUTF(env, plainText)

The C compiler accepts it because jstring is a pointer type (a C++ compiler would not), so plainText will be interpreted as a char const *, which proceeds to do silly things.

Anyway, I suspect that you meant to say

return result;

...but if you meant to return plainText, just say

return plainText;

There's no need to make a copy.

like image 147
Wintermute Avatar answered Oct 03 '22 08:10

Wintermute


There is correct bunch of code here:

JNIEXPORT jstring JNICALL Java_ir_bassir_ndktest4_MainActivity_getName  (JNIEnv *env, jobject obj) {
    jclass cls = (*env)->GetObjectClass(env, obj);
    jmethodID mid = (*env)->GetStaticMethodID(env, cls, "testJava", "(Ljava/lang/String;)Ljava/lang/String;");

    jstring plainText = (*env)->NewStringUTF(env, "Hello from JNI ! Compiled with ABI  2222 ");
    jstring result = (*env)->CallStaticObjectMethod(env, cls, mid, plainText);
    return result;
}
like image 33
bassir pechaz Avatar answered Oct 03 '22 08:10

bassir pechaz