JNI错误(程序错误):访问过时的本地引用0xbc00021(指数8大小为8的表) [英] JNI ERROR (app bug): accessed stale local reference 0xbc00021 (index 8 in a table of size 8)

查看:120
本文介绍了JNI错误(程序错误):访问过时的本地引用0xbc00021(指数8大小为8的表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从绝对初学者和温度转换器应用程序本书的Andr​​oid应用程序Hello World应用程序从这里  无论是在模拟器上运行正常,但当我尝试下面的错误是未来的LogCat中三星注2运行

  7月2日至8日:22:18.665:E / dalvikvm(30944):JNI错误(应用程序错误):访问过时的本地引用0xbc00021(指数8大小为8的表)
七月2日至8日:22:18.665:E / dalvikvm(30944):VM中止
七月2日至8日:22:18.665:A /的libc(30944):致命的信号11(SIGSEGV)在0xdeadd00d(code = 1),螺纹30944(oid.temperature)
 

这两个应用程序做公开展览布局,标题,但没有显示在布局的任何其他意见。

样品运行正常

  

设备:注意2三星gt_n7100

     

IDE:Eclipse的3.8版本

     

操作系统:64位Windows 7的

解决方案

由于Android 4.0的垃圾收集器被改变。现在,它移动的垃圾回收过程中对象周围,这可能会导致很多问题。

假设你有一个静态变量指向一个对象,然后这个对象得到由GC感动。由于Android采用直接指针的Java对象,这将意味着你的静态变量现在指向随机地址的内存,未占用的任何物体或不同种类的对象占用。这几乎保证你会得到EXC_BAD_ACCESS下次使用这个变量的时间。

因此​​Android为您提供了JNI错误(应用程序错误)错误prevent你获得undebugable EXC_BAD_ACCESS。现在有两种方法来避免此错误。

  1. 您可以在您的清单中设置targetSdkVersion到版本11或更低。这将使JNI错误兼容性方式和prevent完全的任何问题。这就是为什么你的老例子是工作的原因。

  2. 您可以尽量避免使用静态变量指向Java对象或使jobject引用全球存储之前通过调用ENV-> NewGlobalRef(REF)。
    也许在这里最大的例子是保持JCLASS对象。通常情况下,你会JNI_OnLoad在初始化静态JCLASS变量,因为类对象留在记忆里,只要在应用程序运行。

这code会导致崩溃:

 静态JCLASS MyClass的;

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM的* VM,无效*保留){
    MyClass的= ENV->的findClass(COM /例子/公司/ MyClass的);
    返回JNI_VERSION_1_6;
}
 

虽然这code将会运行得很好:

 静态JCLASS MyClass的;

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM的* VM,无效*保留){
    JCLASS TMP = ENV->的findClass(COM /例子/公司/ MyClass的);
    MyClass的=(JC​​LASS)ENV-> NewGlobalRef(TMP);
    返回JNI_VERSION_1_6;
}
 

有关更多示例,请参阅马立克Sebera提供的链接:<一href="http://android-developers.blogspot.cz/2011/11/jni-local-reference-changes-in-ics.html">http://android-developers.blogspot.cz/2011/11/jni-local-reference-changes-in-ics.html

I made hello world application from book Android apps for Absolute Beginners and Temperature Convertor app from here Both is running fine on Emulator but when I try to run it on Samsung Note 2 following error is coming on LogCat

02-08 07:22:18.665: E/dalvikvm(30944): JNI ERROR (app bug): accessed stale local reference 0xbc00021 (index 8 in a table of size 8)
02-08 07:22:18.665: E/dalvikvm(30944): VM aborting
02-08 07:22:18.665: A/libc(30944): Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1), thread 30944 (oid.temperature)

Both applications do open shows layout with title but do not shows any other views in layout

Samples runs fine

device: note 2 Samsung-gt_n7100

IDE:Eclipse version 3.8

OS: 64bit Windows 7

解决方案

Since android 4.0 garbage collector was changed. Now it moves object around during garbage collection, which can cause a lot of problems.

Imagine that you have a static variable pointing to an object, and then this object gets moved by gc. Since android uses direct pointers for java objects, this would mean that your static variable is now pointing to a random address in the memory, unoccupied by any object or occupied by an object of different sort. This will almost guarantee that you'll get EXC_BAD_ACCESS next time you use this variable.

So android gives you JNI ERROR (app bug) error to prevent you from getting undebugable EXC_BAD_ACCESS. Now there are two ways to avoid this error.

  1. You can set targetSdkVersion in your manifest to version 11 or less. This will enable JNI bug compatibility mode and prevent any problems altogether. This is the reason why your old examples are working.

  2. You can avoid using static variables pointing to java objects or make jobject references global before storing them by calling env->NewGlobalRef(ref).
    Perhaps on of the biggest examples here is keeping jclass objects. Normally, you'll initialize static jclass variable during JNI_OnLoad, since class objects remain in the memory as long as the application is running.

This code will lead to a crash:

static jclass myClass;

JNIEXPORT jint JNICALL JNI_OnLoad (JavaVM * vm, void * reserved) {  
    myClass = env->FindClass("com/example/company/MyClass");  
    return JNI_VERSION_1_6;  
}

While this code will run fine:

static jclass myClass;

JNIEXPORT jint JNICALL JNI_OnLoad (JavaVM * vm, void * reserved) {  
    jclass tmp = env->FindClass("com/example/company/MyClass");  
    myClass = (jclass)env->NewGlobalRef(tmp);
    return JNI_VERSION_1_6;  
}

For more examples see link provided by Marek Sebera: http://android-developers.blogspot.cz/2011/11/jni-local-reference-changes-in-ics.html

这篇关于JNI错误(程序错误):访问过时的本地引用0xbc00021(指数8大小为8的表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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