我是否需要释放与参数 jobjectArray 关联的任何内存?
我有一个使用以下 JNI 方法的方法
JNIEXPORT void JNICALL Java_com_android_Coordinates_Updates
(
JNIEnv * env,
jobject obj,
jobjectArray coordinates
)
{
int size = env->GetArrayLength(coordinates);
for(int i = 0; i < size; i ++)
{
jfloatArray row = (jfloatArray)env->GetObjectArrayElement(coordinates, i);
jfloat *elements = env->GetFloatArrayElements(row , 0);
//Do I need to release this or any other memory?
env->ReleaseFloatArrayElements( row , (jfloat *)elements, i);
}
}
我的问题是在这种情况下我需要释放任何内存吗?
我问这个问题是因为我有一个 Android 应用程序,可以快速从 Java 到 JNI 传递小数据(触摸坐标)。当我在 Android 设备上快速移动两根手指时,应用程序会在 15 秒内崩溃。我试图缩小问题范围,并发现如果我在上面的方法中包含下面给出的行,应用程序就会崩溃,否则不会。
jfloat *elements = env->GetFloatArrayElements(row , 0);
I have a method with following JNI method
JNIEXPORT void JNICALL Java_com_android_Coordinates_Updates
(
JNIEnv * env,
jobject obj,
jobjectArray coordinates
)
{
int size = env->GetArrayLength(coordinates);
for(int i = 0; i < size; i ++)
{
jfloatArray row = (jfloatArray)env->GetObjectArrayElement(coordinates, i);
jfloat *elements = env->GetFloatArrayElements(row , 0);
//Do I need to release this or any other memory?
env->ReleaseFloatArrayElements( row , (jfloat *)elements, i);
}
}
My question is do I need to release any memory in this case?
I am asking this question because I have an Android app that passes small data(touch coordinates) with a fast pace from Java to JNI. When I move two fingers over my android device in fast speed, app crashes in under 15 seconds. I tried to narrow down the problem and came to the point where if I include line given below in method above app crashes, otherwise it doesn't.
jfloat *elements = env->GetFloatArrayElements(row , 0);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。使用 GetXxxxArrayElements() 撤消工作后,需要调用 ReleaseXxxxArrayElement()。
你有一个数组的数组吗?根据我在您的问题中看到的内容,我猜测下面的应用程序代码可能是什么样子,请修改它重新发布到问题中。
Yes. You need to call ReleaseXxxxArrayElement() when you have used GetXxxxArrayElements() to undo the work.
Do you have an array of an array ? My guess below is what application code might looks like based on what I see in your problem, please modify it repost into the question.