在0xdeadd00d(code = 1),线程17729运行JNI android app A/libc:致命信号11(SIGSEGV)时收到错误消息 [英] I receive error message when run JNI android app A/libc﹕ Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1), thread 17729

查看:134
本文介绍了在0xdeadd00d(code = 1),线程17729运行JNI android app A/libc:致命信号11(SIGSEGV)时收到错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行使用JNI函数和c ++代码的android应用程序时出现错误.运行时,我收到以下消息:

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:

致命信号11(SIGSEGV)位于0xe480001d(代码= 1),线程5465

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

最后是我的代码:

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);
}

在Java方面:

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);
    }
}

推荐答案

此代码的JNI端是C,而不是C ++,并且C对指针修剪的宽松处理是问题的一部分.您的代码在以下两行中断:

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);

因为NewStringUTF具有签名

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)

C编译器会接受它,因为jstring是指针类型(C ++编译器不会),因此plainText将被解释为char const *,从而继续执行愚蠢的事情.

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;

...但是如果您要返回plainText,只需说

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

return plainText;

无需复制.

这篇关于在0xdeadd00d(code = 1),线程17729运行JNI android app A/libc:致命信号11(SIGSEGV)时收到错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆