" JNI ERROR(app bug):弱全局引用表溢出"为什么?

时间:2016-10-13 18:43:17

标签: android-ndk java-native-interface

由于我的本机代码根据此主题泄漏,我一直收到此错误:

ReferenceTable overflow (max=512) JNI

然而,在我看来,AttachCurrentThread泄漏了。我试过这个代码并且它泄漏了

// this code LEAKS!
// C++:
void Engine::UpdateCamera(float x, float y, float z) {
    JNIEnv *jni;
    app_->activity->vm->AttachCurrentThread(&jni, NULL);
    //Do nothing
    app_->activity->vm->DetachCurrentThread();
    return;
}


// Java
public void updateCamera(final float x, final float y, final float z) {
    if (_label2 == null)
        return;

    StackTraceElement trace = new Exception().getStackTrace()[0];
    Log.e(APP_TAG, "Called:" +
            trace.getClassName() + "->" + trace.getMethodName() + ":" + trace.getLineNumber());

}

然后我只是评论了所有内容,程序停止泄漏并永远运行:(

// this code never leaks, but it does not do anything either
void Engine::UpdateCamera(float x, float y, float z) {
    JNIEnv *jni;
    //app_->activity->vm->AttachCurrentThread(&jni, NULL);
    //app_->activity->vm->DetachCurrentThread();
    return;
}

是否有人遇到过AttachCurrentThread的漏洞问题?

谢谢。

2 个答案:

答案 0 :(得分:3)

您是否已连接到调试器?如果是这样,断开连接,您可能会发现弱引用表返回到合理的值。

我有同样的问题;如果我使用调试器运行,则会发生这种情况。

答案 1 :(得分:1)

下面的attatchTestMemoryLeak()函数存在本机内存泄漏,但我仍未弄清原因,但是我确实找到了另一种避免本机内存泄漏的方法。参见函数attatchTestOK();

//c++ code
void attatchTestMemoryLeak(){
    for(int i=0; i<100000; i++){
        JNIEnv *env= nullptr;
        //native thread try to attach java environment;
        int getEnvStat = g_VM->GetEnv((void **)&env,JNI_VERSION_1_4);
        if (getEnvStat == JNI_EDETACHED) {
            jint attachStat=g_VM->AttachCurrentThread(&env, NULL);
            if (attachStat == JNI_OK) {
                LOG_E("index=%d, attach ok",i);
            }else{
                LOG_E("index=%d, attach failed",i);
            }
        }

        //do something, call java function;

        //Detatched the native thread from java environment;
        jint detachStat=g_VM->DetachCurrentThread();
        if(detachStat==JNI_OK){
            LOG_E("detach ok, index=%d, detachStat=%d",i,detachStat);
        }else{
            LOG_E("detach failed, index=%d,detachStat=%d",i,detachStat);
        }
        env = NULL;
    }
}

下面的函数可以正常工作,https://www.jianshu.com/p/1f17ab192940给出了解释。

static pthread_key_t detachKey=0;
void detachKeyDestructor(void* arg)
{
    pthread_t thd = pthread_self();
    JavaVM* jvm = (JavaVM*)arg;
    LOG_E("detach thread, thd=%u",thd);
    jvm->DetachCurrentThread();
}
void attachTestOK(){
    for (int i = 0; i < 1000000; i++)
    {
        JNIEnv *env= nullptr;
        int getEnvStat = g_VM->GetEnv((void **)&env,JNI_VERSION_1_4);
        if (getEnvStat == JNI_EDETACHED) {
            if (detachKey == 0){
                LOG_E("index=%d,create thread key",i);
                pthread_key_create(&detachKey, detachKeyDestructor);
            }

            jint attachStat=g_VM->AttachCurrentThread(&env, NULL);
            pthread_setspecific(detachKey, g_VM_Test);
            if (attachStat == JNI_OK) {
                LOG_E("index=%d, attach ok",i);
            }else{
                LOG_E("index=%d, attach failed",i);
            }
        }
        LOG_E("index=%d, getEnvStat=%d",i,getEnvStat);

        //do something, call java function;

        env = NULL;
    }
}