我有一个相对大的std :: string。我想将它传递给Java而不进行复制。然后传回另一个JNI lib。什么是最好的方法?
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
}
这是一个好方法吗?
答案 0 :(得分:0)
这肯定是一种可行的方法;你甚至可以使用类似的技巧来回传递函数指针。