原生android应用程序SIGSEGV错误

问题描述:

我有一个应用程序使用一些cpp类,但是当我第一次运行我的应用程序时,一切都很好,如果我想再次使用它的停止位置,并且出现此错误: 10-03 19 :50:34.859:A/libc的(1669):在00000000(代码= 1),螺纹1907致命信号11(SIGSEGV)(的AsyncTask#2)原生android应用程序SIGSEGV错误

这是我的代码: 的extern “C” JNIEXPORT jbyteArray JNICALL Java_com_example_qonclient_Codecs_encodeG711(JNIEnv的* ENV,jobject THIZ,jshortArray音){

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;} 

我不知道什么是错。

UPDATE: 有时我得到这个错误: 10-03 20:42:56.741:A/libc的(5975):在0x7a9d3000(代码= 2),螺纹6361(#的AsyncTask致命信号7(SIGBUS)1 )

您正在使用env->SetShortArrayRegion(sound, 0, soundLength, buf);到阵列复制到soundbuf,它就不能成为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); 
} 
+0

非常感谢您! – just 2014-10-03 19:15:22