Search code examples
javaandroidcandroid-ndkjava-native-interface

SIGSEGV Android JNI empty method


I have encountered really weird problem, I have no idea what is wrong, because I have commented out everything, but it still crashes.

Here is my code

Java Part

package net.example.library.arrayprocessor;


public class ArrayProcessor {
    static {
        System.loadLibrary("array-processor");
    }

    public ArrayProcessor() {

    }

    public native short[] process(short[] readBuffer);

}

And I am calling it as follows

   buffer = arrayProcessor.process(buffer);

JNI part CMAke

# Cmake Minimum Version
cmake_minimum_required(VERSION 3.4.1)

add_library(array-processor SHARED
            processor.cpp)
# Link
target_link_libraries(
            array-processor
            android
            log)

processor.cpp

#include <jni.h>
#include <stdio.h>

#define LOGI(...) \
  ((void)__android_log_print(ANDROID_LOG_INFO, "VoiceProcessor::", __VA_ARGS__))

extern "C" {
JNIEXPORT jshortArray JNICALL
Java_net_example_library_arrayprocessor_ArrayProcessor_processor(
        JNIEnv *env, jobject instance, jshortArray readBuffer_);
}

JNIEXPORT jshortArray JNICALL
Java_net_example_library_arrayprocessor_ArrayProcessor_processor(
        JNIEnv *env, jobject instance, jshortArray readBuffer_) {

  /* get size of the array */
  jsize len = env->GetArrayLength(readBuffer_);

  /* get the body of array; it will be referecende by C pointer */
  jshort *body = env->GetShortArrayElements(readBuffer_, 0);

  /* do some stuff */
  for (int i = 0; i < len; i++) {
    printf("Short value: %hd\n", body[i]);

    /* update value */
    body[i] = 0;
  }

  /* release body when you decide it is no longer needed
    Pass changes back to Java */
  env->ReleaseShortArrayElements(readBuffer_, body, JNI_COMMIT);
}

But when I try to call it it throws an error

 A/libc: Fatal signal 11 (SIGSEGV) at 0xc0c5a8f7 (code=1), thread 19335 (pool-3-thread-1)

I have tried to comment out the body of the method, but still the same error.

   A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x6 in tid 3749 (pool-4-thread-1)

What could be wrong with my code ? How to solve and debug such problems ?

I would be grateful for any help


Solution

  • You aren't returning a value. You could return readBuffer_, but the method doesn't need to return a value at all if all it does is modify the input array. It can be declared as void.

    The C compiler should have at least warned you about this. Don't ignore warnings, and don't turn them off. JNI code has to be squeaky-clean.