ReleaseStringUTFChars对于std :: string不起作用

问题描述:

我在jni函数中使用了std :: string,但是我无法使用ReleaseStringUTFChars释放它.我得到的错误是:

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

错误:没有匹配函数可调用NIEnv :: ReleaseStringUTF env-> ReleaseStringUTFChars(path,dir);

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?

这是我的jni函数:

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

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

您需要传递与从 GetStringUTFChars()获得的值完全相同的值.并非如此.您正在传递 std :: string 给您的任何内容,这不一定是同一件事.

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.