Native Crash SIGSEGV в Android JNI

Я получаю signal 11 (SIGSEGV), code 1 (SEGV_MAPERR) Native Crash signal 11 (SIGSEGV), code 1 (SEGV_MAPERR) случайным образом в своем приложении. Приложение перебирает файлы и анализирует их в коде на C ++ и возвращает массив поплавков. Это выполняется в AsyncTask, который выполняется некоторое время при обработке файлов. Что я делаю неправильно в коде, который приводит к сбою? Или это сверхдержавный вопрос? Спасибо.

Это функция AsyncTask doInBackground:

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

Функция между кодом Java и C ++:

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

Функция C ++ 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; } с 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; } с 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; } 

Solutions Collecting From Web of "Native Crash SIGSEGV в Android JNI"

Пожалуйста, дважды проверьте, что вы освобождаете всю память, вы выделяете AND, чтобы вы освобождали объекты памяти / удаления в обратном порядке, например, если вы создали объект A, а затем B, вы должны удалить B, затем A.

  1. Ошибка утечки памяти: float* values не освобождаются в Java_com_superpowered_SuperpoweredPlayer_getInfo ()
  2. Я бы освободил память, выделенную в analyzer-> getresults (), затем floatBuffer и intBuffer, затем удалил анализатор и затем декодер.

Вы уверены, что правильно (и безопасно) освободить память, выделенную в анализаторе-> getresults (), с помощью free (), потому что бесплатно в сверхдержаве могут отличаться от свободного в вашем коде (если либо статически привязана к стандартной библиотеке C) ?