本机Android应用程序中的SIGSEGV错误

本机Android应用程序中的SIGSEGV错误,android,c++,java-native-interface,segmentation-fault,Android,C++,Java Native Interface,Segmentation Fault,我有一个应用程序,它使用一些cpp类,当我第一次运行它时,一切都正常。但如果我想再次使用它,它会停止,我会得到以下错误: 10-03 19:50:34.859:A/libc(1669):0x00000000(代码=1)处的致命信号11(SIGSEGV),线程1907(异步任务#2) 有时我会遇到这样的错误: 10-03 20:42:56.741:A/libc(5975):0x7a9d3000(代码=2)处的致命信号7(SIGBUS),线程6361(异步任务#1) 这是我的密码: extern "

我有一个应用程序,它使用一些
cpp类
,当我第一次运行它时,一切都正常。但如果我想再次使用它,它会停止,我会得到以下错误:

10-03 19:50:34.859:A/libc(1669):0x00000000(代码=1)处的致命信号11(SIGSEGV),线程1907(异步任务#2)

有时我会遇到这样的错误:

10-03 20:42:56.741:A/libc(5975):0x7a9d3000(代码=2)处的致命信号7(SIGBUS),线程6361(异步任务#1)

这是我的密码:

extern "C" JNIEXPORT jbyteArray JNICALL Java_com_example_qonclient_Codecs_encodeG711(JNIEnv* env, jobject thiz, jshortArray sound){

jsize soundLength = env->GetArrayLength(sound);
__android_log_print(ANDROID_LOG_ERROR, "NATIVE", "%d", (int)soundLength);
unsigned char dst[soundLength];
// buffer
jshort *buf;
__android_log_print(ANDROID_LOG_ERROR, "NATIVE", "27"); // my program crash after this line
env->SetShortArrayRegion(sound, 0, soundLength, buf);
__android_log_print(ANDROID_LOG_ERROR, "NATIVE", "29");
G711::ALawEncode(dst, buf , (int)soundLength);//sizeof(shortsound));
__android_log_print(ANDROID_LOG_ERROR, "NATIVE", "30");
jbyteArray result = env->NewByteArray((int)sizeof(dst));
env->SetByteArrayRegion(result, 0, (int)sizeof(dst), (jbyte*)dst);
env->DeleteLocalRef((jobject)buf);
return result;}

如何解决此问题?

您正在使用
env->setShortarray区域(声音,0,声音长度,buf)
将数组
sound
复制到
buf
,不应该是
env->GetShortArrayRegion(sound,0,soundLength,buf)
在这种情况下,您还需要分配
buf
,您当前只需声明
jshort*buf

或者干脆用

jshort* buf = env->GetByteArrayElements(sound, NULL);
if (buf != NULL) {
    G711::ALawEncode(dst, buf , (int)soundLength);
    env->ReleaseByteArrayElements(sound, buf, JNI_ABORT);
}