C ++ JNI本地引用表溢出

时间:2015-02-07 03:55:45

标签: java c++ java-native-interface

我遇到了记忆问题。

这是我的代码。

JNICommunicator.cpp

const char *JNICommunicator::changeStringToUTF8(const char* str)
{
     JniMethodInfo t;
    JniHelper::getStaticMethodInfo(t, JNIPakageName, "changeStringToUTF8", "([B)Ljava/lang/String;");
    jbyteArray arr = (t.env)->NewByteArray(strlen(str));
    (t.env)->SetByteArrayRegion(arr,0,strlen(str), (const jbyte*)str);
    jsize arrSize = t.env->GetArrayLength(arr);
    //jbyte *pbyte = (t.env)->GetByteArrayElements(arr, 0);
    jstring returnValue = (jstring)t.env->CallStaticObjectMethod(t.classID,t.methodID,arr);
    // Release
    //t.env->ReleaseByteArrayElements(arr, pbyte, 0);
    t.env->DeleteLocalRef(arr);
    t.env->DeleteLocalRef(t.classID);
    return t.env->GetStringUTFChars(returnValue,0);
}

JNICommunicator.java

public static String changeStringToUTF8(byte[] arr) throws Exception
{       String ansi = new String(arr, "EUC-KR");
    byte[] utf = ansi.getBytes("UTF-8");
    String ret = new String(utf, "UTF-8");      
    return ret;
}

这是日志。

02-07 12:53:04.195: E/dalvikvm(27764): JNI ERROR (app bug): local reference table overflow (max=512)
02-07 12:53:04.195: W/dalvikvm(27764): JNI local reference table (0x7a5c1d78) dump:
02-07 12:53:04.195: W/dalvikvm(27764):   Last 10 entries (of 512):
02-07 12:53:04.195: W/dalvikvm(27764):       511: 0x4243bf10 java.lang.String "다"
02-07 12:53:04.195: W/dalvikvm(27764):       510: 0x419be8d0 java.lang.Class<java.util.regex.Matcher>
02-07 12:53:04.195: W/dalvikvm(27764):       509: 0x4243c160 java.lang.String "fonts/NanumGothi... (21 chars)
02-07 12:53:04.195: W/dalvikvm(27764):       508: 0x42443240 java.lang.String "다"
02-07 12:53:04.195: W/dalvikvm(27764):       507: 0x4245f370 java.lang.String "졌"
02-07 12:53:04.195: W/dalvikvm(27764):       506: 0x424780d0 java.lang.String "는"
02-07 12:53:04.195: W/dalvikvm(27764):       505: 0x41d7a540 java.lang.String "그"
02-07 12:53:04.195: W/dalvikvm(27764):       504: 0x424a94a0 java.lang.String "다"
02-07 12:53:04.195: W/dalvikvm(27764):       503: 0x424c2200 java.lang.String "뒤"
02-07 12:53:04.195: W/dalvikvm(27764):       502: 0x424deb78 java.lang.String "난"
02-07 12:53:04.195: W/dalvikvm(27764):   Summary:
02-07 12:53:04.195: W/dalvikvm(27764):         3 of java.lang.Class (3 unique instances)
02-07 12:53:04.195: W/dalvikvm(27764):       509 of java.lang.String (509 unique instances)
02-07 12:53:04.195: E/dalvikvm(27764): Failed adding to JNI local ref table (has 512 entries)

我认为返回值需要在cpp文件中释放内存..请帮助!

1 个答案:

答案 0 :(得分:0)

使用GetStringUTFChars解决。

const char *JNICommunicator::changeStringToUTF8(const char* str)
{
     JniMethodInfo t;
    JniHelper::getStaticMethodInfo(t, JNIPakageName, "changeStringToUTF8", "([B)Ljava/lang/String;");
    jbyteArray arr = (t.env)->NewByteArray(strlen(str));
    (t.env)->SetByteArrayRegion(arr,0,strlen(str), (const jbyte*)str);
    jsize arrSize = t.env->GetArrayLength(arr);
    //jbyte *pbyte = (t.env)->GetByteArrayElements(arr, 0);
    jstring returnValue = (jstring)t.env->CallStaticObjectMethod(t.classID,t.methodID,arr);
    const char *nativeString = (t.env)->GetStringUTFChars(returnValue, 0);
    // Release
    //t.env->ReleaseByteArrayElements(arr, pbyte, 0);
    t.env->DeleteLocalRef(arr);
    t.env->DeleteLocalRef(t.classID);
    t.env->DeleteLocalRef(returnValue);
    return nativeString;
}

使用GetStringUTFChars获取const char *并释放jstring。