¿Cómo liberar jstring que se envía desde el código nativo a Java?

I have the following native routine:

void sendMessage(const char* text)
{
    JNIEnv* env;

    if(!_jvm)
        return;

    _jvm->AttachCurrentThread(&env, NULL);

    if(!_nativesCls)
        _nativesCls = env->FindClass("com/foo/BaseLib");
    if(_nativesCls == 0)
            return;

    jstring message = env->NewStringUTF(text);
    if(!_sendStr)
        _sendStr = env->GetStaticMethodID(_nativesCls, "onMessage", "(Ljava/lang/String;)V");
    if(_sendStr)
        env->CallStaticVoidMethod(_nativesCls, _sendStr, message);
    //env->ReleaseStringUTFChars(message, text); // <----- * NOT WORKING
}

If I run this as is, it works fine up until memory fills up and I receive:

ReferenceTable overflow (max=512)

I thought adding the commented line above would fix the issue but it just causes the app to bomb out at that point.

¿Alguna sugerencia?

preguntado el 09 de enero de 11 a las 01:01

1 Respuestas

DeleteLocalRef(). Just like any other Java object that was allocated within JNI. However, it will be automatically garbage-collected once the JNI method returns. Details here: http://download.oracle.com/javase/1.3/docs/guide/jni/spec/design.doc.html#1242

Respondido el 09 de enero de 11 a las 04:01

Awesome, changed it to env->DeleteLocalRef(message); and it worked! Thanks - Nick Gotch

To be clear, it has to return to non-native code, which is why the answer says "the JNI method returns" rather than "the method returns". Sometimes people expect the locals to be cleaned up whenever the current native function returns, regardless of where it's returning to. - fadden

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.