重复调用 JNI 方法会使应用程序崩溃
Repeated calls to JNI method crashes the application
所以我注意到我的应用程序在重复调用以下方法后崩溃了。
JNIEXPORT void JNICALL Java_com_kitware_VolumeRender_VolumeRenderLib_DummyFunction(JNIEnv * env,jobject obj, jlong udp, jdoubleArray rotation, jdoubleArray translation){
jboolean isCopy1, isCopy2 ;
jdouble* rot = env->GetDoubleArrayElements(rotation,&isCopy1);
jdouble* trans = env->GetDoubleArrayElements(translation,&isCopy2);
if(isCopy1 == JNI_TRUE){
env->ReleaseDoubleArrayElements(rotation,rot, JNI_ABORT);
}
if(isCopy2 == JNI_TRUE){
env->ReleaseDoubleArrayElements(translation,trans, JNI_ABORT);
}
}
我认为这是由于某些内存丢失造成的 space 但我确实释放了这里的内存,不是吗?仍然在调用该方法 512 次后,我的应用程序崩溃了。
如果需要,我可以为您提供 Logcat
,但它很长。经过一番调查后,我很确定错误出在内存 allocation/free 进程中(即注释掉两个 GetDoubleArrayElements()
得到一个 运行 应用程序,无论我调用了多少次功能)。
在 android 文档中:http://developer.android.com/training/articles/perf-jni.html
明确说明:
You must Release every array you Get. Also, if the Get call fails, you must ensure that your code doesn't try to Release a NULL pointer later.
据我所知,数字 512 是您的代码超出的本地引用数量限制。所以你应该删除这些检查:if(isCopy2 == JNI_TRUE){
.
不过,上面的文档中有一个关于 JNI_ABORT 的段落,解释了它可能与 isCopy 一起使用 - 但它有点令人困惑。您可以搜索 android 来源以了解如何使用 JNI_ABORT,即这里有一些代码:
在我的代码中,我经常使用 PushLocalFrame/PopLocalFrame 来防止局部引用泄漏。
所以我注意到我的应用程序在重复调用以下方法后崩溃了。
JNIEXPORT void JNICALL Java_com_kitware_VolumeRender_VolumeRenderLib_DummyFunction(JNIEnv * env,jobject obj, jlong udp, jdoubleArray rotation, jdoubleArray translation){
jboolean isCopy1, isCopy2 ;
jdouble* rot = env->GetDoubleArrayElements(rotation,&isCopy1);
jdouble* trans = env->GetDoubleArrayElements(translation,&isCopy2);
if(isCopy1 == JNI_TRUE){
env->ReleaseDoubleArrayElements(rotation,rot, JNI_ABORT);
}
if(isCopy2 == JNI_TRUE){
env->ReleaseDoubleArrayElements(translation,trans, JNI_ABORT);
}
}
我认为这是由于某些内存丢失造成的 space 但我确实释放了这里的内存,不是吗?仍然在调用该方法 512 次后,我的应用程序崩溃了。
如果需要,我可以为您提供 Logcat
,但它很长。经过一番调查后,我很确定错误出在内存 allocation/free 进程中(即注释掉两个 GetDoubleArrayElements()
得到一个 运行 应用程序,无论我调用了多少次功能)。
在 android 文档中:http://developer.android.com/training/articles/perf-jni.html
明确说明:
You must Release every array you Get. Also, if the Get call fails, you must ensure that your code doesn't try to Release a NULL pointer later.
据我所知,数字 512 是您的代码超出的本地引用数量限制。所以你应该删除这些检查:if(isCopy2 == JNI_TRUE){
.
不过,上面的文档中有一个关于 JNI_ABORT 的段落,解释了它可能与 isCopy 一起使用 - 但它有点令人困惑。您可以搜索 android 来源以了解如何使用 JNI_ABORT,即这里有一些代码:
在我的代码中,我经常使用 PushLocalFrame/PopLocalFrame 来防止局部引用泄漏。