Android JNI中的本地崩溃SIGSEGV

6

我在我的应用程序中遇到了一个随机的本地崩溃信号11(SIGSEGV),代码1(SEGV_MAPERR)。该应用循环遍历文件,并在C++代码中对它们进行分析,返回一个浮点数数组。这是在AsyncTask中完成的,它会在处理文件时运行一段时间。我的代码有什么问题导致了崩溃?还是这是一个Superpowered的问题?谢谢。

下面是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;

}

1
堆栈跟踪可以提供帮助。使用ndk-stack,您可以指向C++中失败的行。 - Alex Cohn
1
它有时能正常工作,有时却不能,这表明在运行时获得的值存在问题。这可能是从Java传递的路径——这里使用的“url”值: const char *openError = decoder->open(url, false, 0, 0); if (openError) { delete decoder; return NULL; };在这种情况下,这一行可能会导致SIGSEGV: env->SetFloatArrayRegion(ret, 0, 2, values);通过添加日志消息来定位问题。这可能会准确定位问题。 - user2995358
1
@user2995358 感谢您的回复。您提到的 URL 问题确实有道理。但是,在 Java 代码中已经进行了非空检查。此外,该问题在运行时随机发生,而不是在同一“url”项上。我猜测可能是我没有正确处理内存,导致出现了 SIGSEGV 错误。 - Ziad Halabi
1
@ZiadHalabi 它不一定是空的,但URL可能不是正确的格式。SIGSEGV发生是因为您正在访问不属于您的内存或使用未初始化的变量。检查所有指针是否为空,这些指针应该从解码器/分析器方法中获取值。例如,openError不为空,请在使用bpm和keyIndex之前进行初始化等。 - user2995358
1个回答

3
请务必仔细检查您是否释放了所有内存,并按照相反的顺序分配并释放内存/删除对象,例如,如果您创建了对象A然后是B,则应该先删除B再删除A。
  1. 内存泄漏:在Java_com_superpowered_SuperpoweredPlayer_getInfo()中没有释放float* values
  2. 我会首先释放analyzer->getresults()中分配的内存,然后释放floatBuffer和intBuffer,然后删除analyzer,最后是decoder。
您确定使用free()释放在analyzer->getresults()中分配的内存是正确和安全的吗?因为在superpowered中的free可能与您的代码中链接到静态C库的free不同。

在我的看法中,SuperpoweredPlayer::getKey函数中的ret=(float*)malloc(2*sizeof(float)); 似乎从未被释放。(例如在env->SetFloatArrayRegion(ret, 0, 2, values);之后) - Yoni Gross
这是答案中的第1项。 - ivan_onys
是的。我认为你指出的肯定是一个内存泄漏,但这并不十分明确。也许应该将“values”替换为float *values,并且指出这是一个内存泄漏。 - Yoni Gross
@YoniGross 完成 - ivan_onys
@ivan_onys 谢谢你的回答。你能告诉我为什么释放内存的顺序很重要吗? - Ziad Halabi
分析器和解码器可能仍在使用缓冲区,但缓冲区已被释放。应该保留分配的缓冲区,直到这些缓冲区的用户存在。最直接的方法是按照它们的分配/创建顺序的相反顺序删除对象/释放内存。 - ivan_onys

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接