Problems with accessed stale local reference appearing in JNI calls in Android

Source: Internet
Author: User

Before doing a native module encountered such a problem:

The code runs on android2.3 without any problems, but running the Times on 4.2: JNI error (App bug): accessed stale local reference error.

The answer to the question was later found on the StackOverflow. In short, more than 4.0 of the Android system GC in the garbage collection in order to reduce memory fragmentation, the memory will be organized, the memory of the object will inevitably move the address, when the C code pointer also points to the original object address, then the object has been moved to another location, so there will be problems.

The solution can be found in the original:

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 is a static variable pointing to an object, and the This object gets the moved by GC. Since Android uses direct pointers for Java objects, this would mean then 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 would almost guarantee that you'll get exc_bad_access next time you use this variable.

So Android gives your JNI error (App bug) error to prevent-getting undebugable exc_bad_access. Now there is the ways to avoid this error.

    1. You can set targetsdkversion in your manifest to version one or less. This would enable JNI bug compatibility mode and prevent any problems altogether. This is the reason so your old examples is working.

    2. You can avoid using the static variables pointing to Java objects or make jobject references global before storing them by Cal Ling Env->newglobalref (ref).
      Perhaps on the biggest examples this 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 would leads 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 the this code would 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

Problems with accessed stale local reference appearing in JNI calls in Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.