当我们使用传递给函数的最后一个变量时,我有这个JNI函数崩溃(SIGSEGV 11)。据我所知,它没有任何内在错误。我的绑带解决方案是添加第三个变量名称(示例是低位)并且根本不使用第三个变量名称。 (改变了一些变量名称以保护无辜者)
JNIEXPORT jfloatArray JNICALL Java_com_example_core_lib_nativeJavaCall
(JNIEnv * env, jclass, jlong nativeInstance, jlongArray listItems) {
float* resultStorage1;
float* resultStorage2;
/// Do some unrelated things ...
std::vector<LibClass> listofLibClasses = initialzeList(); //Create list fine
int statusCode = MyLib::coreCall(*libClassInstance, listOfLibClasses,
resultStorage1, resultStorage2);
// Add resultStorage1/2 to a jFloat array to give back to Java
return <AjfloatArray>;
}
还有coreCall,这是你的花园式C ++:
int coreCall(LibClass *libClassInstance, std::vector<LibClass> listOfLibClasses,
float *result1, float *result2) {
float
.... run some calculations
int resultCode = doSomething();
(*result1) = localFloatVariable; //this works fine and doesn't crash
(*result2) = otherLocalFloatVariable; //This one segfaults consistently.
....
return resultCode;
}
&#34;修复&#34;就像这样,注意传递给coreCall
的额外变量:
JNIEXPORT jfloatArray JNICALL Java_com_example_core_lib_nativeJavaCall
(JNIEnv * env, jclass, jlong nativeInstance, jlongArray listItems) {
float* resultStorage1;
float* resultStorage2;
float* paddingFullOfWat;
/// Do some unrelated things ...
std::vector<LibClass> listofLibClasses = initialzeList(); //Create list fine
int statusCode = MyLib::coreCall(*libClassInstance, listOfLibClasses,
resultStorage1, resultStorage2, paddingFullOfWat);
// Add resultStorage1/2 to a jFloat array to give back to Java
return <AjfloatArray>;
}
// ** //
int coreCall(LibClass *libClassInstance, std::vector<LibClass> listOfLibClasses, float
*result1, float *result2, float *padding) { //Notice the extra float
.... run some calculations
int resultCode = doSomething();
(*result1) = localFloatVariable; //this works fine and doesn't crash
(*result2) = otherLocalFloatVariable; //now this works
....
return resultCode;
}
我已经使用调试语句将崩溃范围缩小到原始调用中的(*result2) = otherLocalFloatVariable;
。我还尝试向后传递原始变量,因为无论如何它们都是浮点数,并且只要使用result2
它就会崩溃。我在想一个编译器/环境错误?在Macbook Pro上进行测试时,此代码运行正常,我正在使用运行Unix的Dell计算机进行编译并将其加载到Android手机上。任何见解都将不胜感激。