JNI传递ByteArray参数使用如下方式出现异常:
jsize length = env->GetArrayLength(data);jbyte* initBytes = env->GetByteArrayElements(data, nullptr);char* initBuffer = new char[length];memcpy(initBuffer, initBytes, length);env->ReleaseByteArrayElements(data, initBytes, JNI_ABORT);
调试时发现JNI打印出的initBuffer比JAVA打印的data后面会多处几个字符,可能是data长度出现了改变,修改代码:
jsize length = env->GetArrayLength(data);jbyte* initBytes = env->GetByteArrayElements(data, nullptr);char* initBuffer = new char[length];memcpy(initBuffer, initBytes, length);//在复制的数据末尾添加一个空字符'\0',添加字符串终止符:initBuffer[length] = 0;env->ReleaseByteArrayElements(data, initBytes, JNI_ABORT);