我是否需要释放与参数 jobjectArray 关联的任何内存?

发布于 2024-12-09 12:14:18 字数 843 浏览 0 评论 0原文

我有一个使用以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聆听风音 2024-12-16 12:14:18

是的。使用 GetXxxxArrayElements() 撤消工作后,需要调用 ReleaseXxxxArrayElement()。

// why did you use "i" ?
env->ReleaseFloatArrayElements( row , (jfloat *)elements, 0);
// we cleanup local ref due to looping ("size" might be large!)
env->DeleteLocalRef(row);

你有一个数组的数组吗?根据我在您的问题中看到的内容,我猜测下面的应用程序代码可能是什么样子,请修改它重新发布到问题中。

Float[] firstElement = new Float[2];
firstElement[0] = 1.0f;   // x value 
firstElement[1] = 42.0f;  // y value

Object[] coordinates = new Object[1];
coordinates[0] = firstElement;

com.android.Coordinates coordObject = new com.android.Coordinates();
coordObject.Updates(coordinates);

Yes. You need to call ReleaseXxxxArrayElement() when you have used GetXxxxArrayElements() to undo the work.

// why did you use "i" ?
env->ReleaseFloatArrayElements( row , (jfloat *)elements, 0);
// we cleanup local ref due to looping ("size" might be large!)
env->DeleteLocalRef(row);

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.

Float[] firstElement = new Float[2];
firstElement[0] = 1.0f;   // x value 
firstElement[1] = 42.0f;  // y value

Object[] coordinates = new Object[1];
coordinates[0] = firstElement;

com.android.Coordinates coordObject = new com.android.Coordinates();
coordObject.Updates(coordinates);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文