android jni crash 分析工具,java – Android JNI中的Native Crash SIGSEGV

博客讨论了一个Android应用程序在遍历文件并使用C代码进行音频分析时遇到的NativeCrash问题,具体表现为SIGSEGV信号11错误。问题可能与内存管理、资源释放或线程安全有关。在后台线程(AsyncTask)中执行音频分析,可能由于长时间运行导致内存泄漏或资源耗尽。解决方案可能涉及优化C代码的内存分配和释放,以及确保Java和C代码之间的交互正确处理异常。
摘要由CSDN通过智能技术生成

我在我的应用程序中随机获得Native Crash信号11(SIGSEGV),代码1(SEGV_MAPERR). App循环遍历文件并在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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值