0
我有一个相对较大的std :: string。我想将它传递给Java而不复制。然后传回另一个JNI库。什么是最好的方法?Android JNI:将std :: string传递给Java并返回到C++
jlong some_jni_call() {
string str = createLargeString(); // say this is from 3rd lib only returns string
string* strInHeap = new string(str); // this should just increase the reference count?
jlong handle = (long)strInHeap;
return handle;
}
这样我就可以回传给JNI:
void use_string(jlong handle) {
string* str = (string*)handle;
// use the str...
delete str; // doesn't look so nice, since people can forget to delete
}
这是一个好办法?
'string * strInHeap = new string(str)'创建'str'的一个副本。 std:.string不是引用计数。 –
它似乎是某些情况下的引用计数:http://stackoverflow.com/questions/12520192/is-stdstring-refcounted-in-gcc-4-x-c11 – counter