gpt4 book ai didi

java - 我是否需要释放我修改并返回的 JNI 数组?

转载 作者:行者123 更新时间:2023-12-01 13:47:17 24 4
gpt4 key购买 nike

我是 JNI 的新手,在我测试的一部手机上,我收到有关 ReferenceTable 溢出 的错误。我知道这与不在 native 代码中释放 JNI/Java 项有关。

我认为我不需要释放任何东西,因为我使用给定的jfloatarray,替换它的值,然后再次返回原始的JNI对象。我认为这将使 JNI 释放对象本身,因为它只是将其取回。

JNI 代码:

extern "C"
JNIEXPORT jfloatArray JNICALL Java_methodname(
JNIEnv *env, jobject thiz, jfloatArray data)
{
float* nativeValues = (float *)env->GetFloatArrayElements(data, 0);
doSomething(nativeValues);//overwrite the values with new values
return data;
}

JNI 错误(Android 2.3):

11-28 15:00:58.069: W/dalvikvm(833): ReferenceTable overflow (max=1024)
11-28 15:00:58.069: W/dalvikvm(833): Last 10 entries in JNI pinned array reference table:
11-28 15:00:58.069: W/dalvikvm(833): 1014: 0x405f3750 cls=[F (20 bytes)
11-28 15:00:58.069: W/dalvikvm(833): 1015: 0x405f37b0 cls=[F (20 bytes)
11-28 15:00:58.069: W/dalvikvm(833): 1016: 0x405f3810 cls=[F (20 bytes)
11-28 15:00:58.069: W/dalvikvm(833): 1017: 0x405f3870 cls=[F (20 bytes)
11-28 15:00:58.069: W/dalvikvm(833): 1018: 0x405f38d0 cls=[F (20 bytes)
11-28 15:00:58.069: W/dalvikvm(833): 1019: 0x405f3930 cls=[F (20 bytes)
11-28 15:00:58.069: W/dalvikvm(833): 1020: 0x405f3990 cls=[F (20 bytes)
11-28 15:00:58.069: W/dalvikvm(833): 1021: 0x405f39f0 cls=[F (20 bytes)
11-28 15:00:58.069: W/dalvikvm(833): 1022: 0x405f3a50 cls=[F (20 bytes)
11-28 15:00:58.069: W/dalvikvm(833): 1023: 0x405f3ab0 cls=[F (20 bytes)
11-28 15:00:58.074: W/dalvikvm(833): JNI pinned array reference table summary (1024 entries):
11-28 15:00:58.074: W/dalvikvm(833): 1020 of [F 20B (1020 unique)
11-28 15:00:58.074: W/dalvikvm(833): 4 of [F 28B (4 unique)
11-28 15:00:58.074: W/dalvikvm(833): Memory held directly by tracked refs is 20512 bytes
11-28 15:00:58.074: E/dalvikvm(833): Failed adding to JNI pinned array ref table (1024 entries)

我该怎么办?将数据值复制到新的 float* 并用它创建一个新的 jfloatarray

最佳答案

当您通过 GetFloatArrayElements(..) 函数获取元素时,由于 JVM 将其标记为“在 native 代码中使用”,因此您需要释放数组元素。为此调用 ReleaseFloatArrayElements() 。在那里查看详细信息: http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/functions.html#wp17440

关于java - 我是否需要释放我修改并返回的 JNI 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20268170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com