JNI ERROR (App bug): accessed stale local reference 0xbc00021

Source: Internet
Author: User

When programming with the Android NDK, you use the. so file, so files are written in the C language. When I call the Java class in the C file, the first call is fine, but the second call fails. Internet search a lot of information, presumably because in the JNI, using the pointer to a Java object, because of the Android garbage collection mechanism (garbage Collector), if the Java object is recycled, then the pointer to the object will be empty or not exist, This suggests that the JNI error:accessed stale (stale, backward) local reference probably means that the variable is no longer there. Therefore, to solve this problem, it is required to define the Java object as static, so as to avoid being recycled (after Android4.0, the static variables will be recycled, but the probability is small), resulting in the creation of errors. Here is my example:

Jobject Mtestprovider;

Mtestprovider = (*jnienv)->newobject (jnienv, Obj_class, construction_id);

After modification:

Static Jobject Mtestprovider;

Jobject obj = jobject obj = (*jnienv)->newobject (jnienv,obj_class,construction_id);

Mtestprovider = (jobject) (*jnienv)->newglobalref (jnienv,obj);

The above is only personal experience, hope to solve your problem!

JNI ERROR (App bug): accessed stale local reference 0xbc00021

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.