Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Native Crash SIGSEGV in Android JNI

I am getting a Native Crash signal 11 (SIGSEGV), code 1 (SEGV_MAPERR) randomly in my App. The App loops over files and analyses them in the C++ code and return an array of floats. This is done in an AsyncTask which runs for a while while processing the files. What am I doing wrong in the code which is leading to the crash? Or is it a Superpowered issue? Thank you.

This is the AsyncTask doInBackground function:

protected String doInBackground(Object... urls) {

            for (int i = 0; i < songFiles.size(); i++) {
                SongFile temp = songFiles.get(i);
                try {
                    float[] f = Analyser.getInfo(temp.getPath());
                        if (f != null && f.length > 1) {
                            ...save to DB
                        }
                    }

                } catch (Exception e) {
                }
            }
            return "";
        } 

The Function between the Java and C++ code:

extern "C" JNIEXPORT jfloatArray Java_com_superpowered_SuperpoweredPlayer_getInfo(JNIEnv *env, jobject instance,jstring filepath) {
    jfloatArray ret;
    char *Path= (char *) env->GetStringUTFChars(filepath, JNI_FALSE);

    ret = (jfloatArray)env->NewFloatArray(2);

    float *values = superpoweredPlayer->getKey(Path);

    env->SetFloatArrayRegion(ret, 0, 2, values);
    env->ReleaseStringUTFChars(filepath, Path);
    return ret;

}

The C++ function getKey:

float *SuperpoweredPlayer::getKey(char *url) {

    SuperpoweredDecoder *decoder = new SuperpoweredDecoder();

    //decoder initialize from the URL input
    const char *openError = decoder->open(url, false, 0, 0);
    if (openError) {
        delete decoder;
        return NULL;
    };

    // Create the analyzer.
    SuperpoweredOfflineAnalyzer *analyzer = new SuperpoweredOfflineAnalyzer(decoder->samplerate, 0, decoder->durationSeconds);

    // Create a buffer for the 16-bit integer samples coming from the decoder.
    short int *intBuffer = (short int *)malloc(decoder->samplesPerFrame * 2 * sizeof(short int) + 16384);
    // Create a buffer for the 32-bit floating point samples required by the effect.
    float *floatBuffer = (float *)malloc(decoder->samplesPerFrame * 2 * sizeof(float) + 1024);

    // Processing.
    while (true) {

        // Decode one frame. samplesDecoded will be overwritten with the actual decoded number of samples.
        unsigned int samplesDecoded = decoder->samplesPerFrame;
        if (decoder->decode(intBuffer, &samplesDecoded) == SUPERPOWEREDDECODER_ERROR) break;
        if (samplesDecoded < 1) break;

        // Convert the decoded PCM samples from 16-bit integer to 32-bit floating point.
        SuperpoweredShortIntToFloat(intBuffer, floatBuffer, samplesDecoded);

        // Submit samples to the analyzer.
        analyzer->process(floatBuffer, samplesDecoded);

        // Update the progress indicator.
        // progress = (double)decoder->samplePosition / (double)decoder->durationSamples;
    };



    // Get the result.
    unsigned char *averageWaveform = NULL, *lowWaveform = NULL, *midWaveform = NULL, *highWaveform = NULL, *peakWaveform = NULL, *notes = NULL;
    int waveformSize, overviewSize, keyIndex;
    char *overviewWaveform = NULL;
    float loudpartsAverageDecibel, peakDecibel, bpm, averageDecibel, beatgridStartMs = 0;
    analyzer->getresults(&averageWaveform, &peakWaveform, &lowWaveform, &midWaveform, &highWaveform, &notes, &waveformSize, &overviewWaveform, &overviewSize, &averageDecibel, &loudpartsAverageDecibel, &peakDecibel, &bpm, &beatgridStartMs, &keyIndex);

    float *ret;
    ret=(float*)malloc(2*sizeof(float));
    ret[0] = bpm;
    ret[1] = keyIndex;

    // Cleanup.
    delete decoder;
    delete analyzer;
    free(intBuffer);
    free(floatBuffer);

    // Done with the result, free memory.
    if (averageWaveform) free(averageWaveform);
    if (lowWaveform) free(lowWaveform);
    if (midWaveform) free(midWaveform);
    if (highWaveform) free(highWaveform);
    if (peakWaveform) free(peakWaveform);
    if (notes) free(notes);
    if (overviewWaveform) free(overviewWaveform);

    return ret;

}
like image 730
Ziad Halabi Avatar asked Jan 11 '17 18:01

Ziad Halabi


People also ask

What is Native crash Android?

An app that is written using native-code languages crashes if there's an unhandled signal, such as SIGSEGV, during its execution. When an app crashes, Android terminates the app's process and displays a dialog to let the user know that the app has stopped, as shown in figure 1.

What is Tombstone crash?

Crash dumps and tombstones The tombstone is a file with extra data about the crashed process. In particular, it contains stack traces for all the threads in the crashing process (not just the thread that caught the signal), a full memory map, and a list of all open file descriptors.

What is JNI in Android R?

JNI is the Java Native Interface. It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++).

What is NDK and JNI?

JNI is just the way that Java handles calling into native/C++ code, and calling back into Java from there. It has nothing to say about Android - it is a Java language feature. The Android NDK is a way to write Android applications using code called by JNI.


1 Answers

Please, double check that you are releasing all the memory, you are allocating AND that you release memory/delete objects in the reverse order e.g. if you created object A then B, you should delete B then A.

  1. Memory leak: float* values isn't freed in Java_com_superpowered_SuperpoweredPlayer_getInfo()
  2. I would free memory allocated in analyzer->getresults() first, then floatBuffer and intBuffer, then deleted analyzer and then decoder.

Are you sure it is correct (and safe) to free memory allocated in analyzer->getresults() with free(), because free in the superpowered may be different from free in your code (if either is linked statically to standard C library)?

like image 153
ivan_onys Avatar answered Sep 30 '22 12:09

ivan_onys