gpt4 book ai didi

java - Android JNI 中的 native 崩溃 SIGSEGV

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:08:16 25 4
gpt4 key购买 nike

我在我的应用程序中随机收到 native 崩溃 信号 11 (SIGSEGV),代码 1 (SEGV_MAPERR)。该应用程序循环处理文件并在 C++ 代码中分析它们并返回一个 float 数组。这是在处理文件时运行一段时间的 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;

}

最佳答案

请仔细检查您是否正在释放所有内存,是否正在分配以及是否以相反的顺序释放内存/删除对象,例如如果您创建对象 A 然后 B,您应该删除 B 然后 A。

  1. 内存泄漏:float* values 未在 Java_com_superpowered_SuperpoweredPlayer_getInfo() 中释放
  2. 我会先释放在 analyzer->getresults() 中分配的内存,然后是 floatBuffer 和 intBuffer,然后是删除分析器,最后是解码器。

您确定使用 free() 释放在 analyzer->getresults() 中分配的内存是正确的(且安全的)吗,因为超能力中的 free 可能与代码中的 free 不同(如果两者都静态链接到标准C 库)?

关于java - Android JNI 中的 native 崩溃 SIGSEGV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41597950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com