我在托管代码中创建了一个缓冲区:
var nativeBuffer = ShortArray(bufferSize)
这是Kotlin代码,但是根据stdlib documentation,此语法实际上是返回
short []
的包装器。然后,我想使用JNI从C++写入此缓冲区:
JNIEXPORT void JNICALL Java_package_class_name_readNext
(JNIEnv * env, jclass clazz, jshortArray javaArray)
{
jboolean * isCopy;
short * targetArray = env->GetShortArrayElements(javaArray, isCopy);
// Do stuff to targetArray
}
问题在于
GetShortArrayElements
调用始终会导致崩溃,并带有以下逻辑删除:11-07 21:23:53.610 86-86/? I/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
11-07 21:23:53.610 86-86/? I/DEBUG: Build fingerprint: 'generic/vbox86p/vbox86p:5.1/LMY47D/buildbot06092001:userdebug/test-keys'
11-07 21:23:53.610 86-86/? I/DEBUG: Revision: '0'
11-07 21:23:53.610 86-86/? I/DEBUG: ABI: 'x86'
11-07 21:23:53.610 86-86/? I/DEBUG: pid: 28339, tid: 28477, name: Thread-282 >>> <package-name> <<<
11-07 21:23:53.610 86-86/? I/DEBUG: signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0xb3bcfacb
11-07 21:23:53.613 86-86/? I/DEBUG: eax b3bcfacb ebx b3ffcaa4 ecx a2497000 edx b426e480
11-07 21:23:53.613 86-86/? I/DEBUG: esi a2497000 edi b3fff440
11-07 21:23:53.613 86-86/? I/DEBUG: xcs 00000073 xds 0000007b xes 0000007b xfs 000000e7 xss 0000007b
11-07 21:23:53.613 86-86/? I/DEBUG: eip b3dcd69c ebp 9f5a2968 esp 9f5a2880 flags 00210282
11-07 21:23:53.613 86-86/? I/DEBUG: #00 pc 002e069c /system/lib/libart.so (short* art::JNI::GetPrimitiveArray<_jshortArray*, short, art::mirror::PrimitiveArray<short> >(_JNIEnv*, _jshortArray*, unsigned char*)+572)
11-07 21:23:53.613 86-86/? I/DEBUG: #01 pc 0010cdad /system/lib/libart.so (art::CheckJNI::GetShortArrayElements(_JNIEnv*, _jshortArray*, unsigned char*)+125)
javaArray
不为空(通常为-1621480864之类的地址),并且nativeBuffer
是静态变量,因此不太可能被GC处理。该行为发生在Genymotion实例(x86)和ARM设备上,但我仅针对Android 5.0+进行了测试。我还能错过什么? 最佳答案
糟糕,您没有正确使用isCopy
参数。它应该是:
jboolean isCopy;
jshort* targetArray = env->GetShortArrayElements(javaArray, &isCopy);
也就是说,
isCopy
是一个“输出参数”(使用C#语言)。如果您不在乎
targetArray
是否别名为javaArray
,也可以改为传递nullptr
,如下所示:jshort* targetArray = env->GetShortArrayElements(javaArray, nullptr);