Search code examples
c++java-native-interfacestdstring

ReleaseStringUTFChars not working for std::string


I use std::string in my jni function, and I fail to release it using ReleaseStringUTFChars. The error I get is:

error: no matching function for call to NIEnv::ReleaseStringUTF env->ReleaseStringUTFChars(path, dir);

I understand, that instead of string, the function expects to get char, but I don't have such variable. What should I do?

This is my jni function:

void Java_com_example_android_OpenCVActivity_test (JNIEnv * env, jclass clazz, jstring path){

    std::string dir = env->GetStringUTFChars(path, 0);
   ....
env->ReleaseStringUTFChars(path, dir);
}

Solution

  • You need to pass it exactly the same value you got from GetStringUTFChars(). You're not. You're passing whatever std::string gives you, which isn't necessarily the same thing.