2016-09-15 81 views
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 
} 

这是一个好办法?

+0

'string * strInHeap = new string(str)'创建'str'的​​一个副本。 std:.string不是引用计数。 –

+0

它似乎是某些情况下的引用计数:http://stackoverflow.com/questions/12520192/is-stdstring-refcounted-in-gcc-4-x-c11 – counter

回答

0

这当然是一种可行的方法;你甚至可以使用类似的技巧来回传递函数指针。

相关问题