SIGSEGV error in native Android application

-1

I have an application which use some cpp class and when I first run it everything is OK. But if I would like to use it again, it stops and I get this error:

10-03 19:50:34.859: A/libc(1669): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 1907 (AsyncTask #2)

Sometimes I get this error:

10-03 20:42:56.741: A/libc(5975): Fatal signal 7 (SIGBUS) at 0x7a9d3000 (code=2), thread 6361 (AsyncTask #1)

Here is my code:

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

How to fix this?

android
c++
jni
sigsegv
asked on Stack Overflow Oct 3, 2014 by just • edited Sep 15, 2018 by scopchanov

1 Answer

0

You are using env->SetShortArrayRegion(sound, 0, soundLength, buf); to copy the array sound to buf, shouldn't that be env->GetShortArrayRegion(sound, 0, soundLength, buf); In this case, you will also need to allocate buf, you currently declare simply jshort *buf.

Or simply use

jshort* buf = env->GetByteArrayElements(sound, NULL);
if (buf != NULL) {
    G711::ALawEncode(dst, buf , (int)soundLength);
    env->ReleaseByteArrayElements(sound, buf, JNI_ABORT);
}
answered on Stack Overflow Oct 3, 2014 by ashoke

User contributions licensed under CC BY-SA 3.0