Android ICS : JNI error attempt to use stale local reference 0x1

前端 未结 6 623
终归单人心
终归单人心 2021-02-02 11:50

After upgrading my phone to android 4.03 ics my game dosent open anymore ,it just closes without any error messege on deviCe and with this on eclipse

04-02 16:         


        
6条回答
  •  梦谈多话
    2021-02-02 12:32

    @Ernest and @Ilya are correct. Note that the problem manifests itself in other ways too, not just signature mismatches. There is another very specific case that I ran into, which was explained here in the Android' team's blog under the section:

    Bug: Mistakenly assuming FindClass() returns global references

    FindClass() returns local references. Many people assume otherwise. In a system without class unloading (like Android), you can treat jfieldID and jmethodID as if they were global. (They’re not actually references, but in a system with class unloading there are similar lifetime issues.) But jclass is a reference, and FindClass() returns local references. A common bug pattern is “static jclass”. Unless you’re manually turning your local references into global references, your code is broken.

    Basically I was caching the jclass returned by FindClass in a global variable as is, but it turns out this value (as of Android 4?) is now a localref. As a result, I had to convert it into a globalref like so:

    jclass jc = env->FindClass(callbacks.name);
    // Since Android ICS, class references are not global so we need to peg a
    // global reference to the jclass returned by FindClass(), otherwise we get
    // following error in the log:
    // "JNI ERROR (app bug): attempt to use stale local reference 0xHHHHHHHH".
    callbacks._class = static_cast(env->NewGlobalRef(jc));
    

    After a lot of head-scratching, this fixed the issue for me.

提交回复
热议问题