Issue
I am calling into my native C library using the NDK from my Activity. When calling from the main UI thread everything works fine (but the UI is blocked). When calling from a new thread (either created with new Thread()
or using AsyncTask
) it crashes with a "A/libc(32044): Fatal signal 11 (SIGSEGV) at 0x77f1a000 (code=2), thread 32624 (Thread-56564)" error in logcat.
I make two calls in the library. The first call executes ok. The second call crashes on the GetByteArrayRegion statement:
JNIEXPORT void JNICALL Java_com_mycompany_myproduct_library_process_data(JNIEnv *env, jobject obj, jbyteArray jBuffer) {
int len = (*env)->GetArrayLength(env, jBuffer);
unsigned char buffer[len];
(*env)->GetByteArrayRegion(env, jBuffer, 0, len, buffer); // crash
process_buffer(buffer);
}
The reason I am trying to run in a separate thread is to not block the UI during execution and to show a progress bar.
Any help is greatly appreciated.
Solution
As @BitBank pointed out correctly, the declaration of buffer[len] does not allocate the memory and unallocated memory is used for the buffer (probably on the stack). This only became visible to me when using a different thread. Changing it to
unsigned char *buffer;
buffer = malloc(len);
(*env)->GetByteArrayRegion(env, jBuffer, 0, len, buffer);
process_buffer(buffer);
free(buffer);
solved the issue.
Answered By - mvandillen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.