欢迎光临
我们一直在努力

程序员Android JNI中的本机崩溃SIGSEGV – java

程序员面试经验

我在我的应用程序中随机获取本机崩溃signal 11 (SIGSEGV), code 1 (SEGV_MAPERR)。该应用程序循环遍历文件,并在C++代码中对其进行分析,并返回一个浮点数组。这是在AsyncTask中完成的,该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。

  • 内存泄漏:Java_com_superpowered_SuperpoweredPlayer_getInfo()中未释放float* values
  • 我先释放在analyzer-> getresults()中分配的内存,然后释放floatBuffer和intBuffer,再删除analyzer,然后解码器。
  • 您确定使用free()释放在analyzer-> getresults()中分配的内存是正确(安全)的,因为超能力中的free可能与代码中的free有所不同(如果两者静态链接到标准C库) ?

    android-android studio模拟器中的SSL问题,在手机上可以正常工作 – java

    我有一个可以通过https调用网络服务的应用程序。当我在手机上运行apk时,效果很好。但是,在模拟器中,所有通过POST的SSL请求均失败,并显示以下信息: 读取错误:ssl = 0xb402be00:SSL库失败,通常是一个协议 错误 错误:100c50bf:SSL例程:ssl3_read_bytes:NO_RENEGOTIATION(外部/无聊的sl /…

    绑定Java库Xamarin.Android – java

    我花了两天时间在每个论坛,文档,tuto,博客等上寻找答案。我为实习生启动了一个Android应用程序,因为我不懂Java,所以用xamarin C#开发了它。直到最近一切都还不错,但现在我需要集成一个SDK才能在应用程序中使用POS(销售点),但是该库是用Java编写的,即使跟随文档或辅导老师,我也无法将其与xamarin绑定(我什至无法调试)。这里有人已…

    Android Studio报告“android sdk解析不正确” – java

    我只是将一个android项目从eclipse迁移到了android studio,但是当我尝试对其进行编译时,出现错误“android sdk被错误地解析”。有什么帮助吗? 参考方案 尝试将Android Studio配置为指向您的SDK安装。

    首先关注EditText清晰文本-Android – java

    It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current fo…

    在Android中以编程方式从启动器中删除应用程序 – java

    有没有一种方法可以在运行时从家庭启动器中删除活动?我的意思是从其属性或类似内容中删除Intent.CATEGORY_LAUNCHER。 参考方案 您可以通过PackageManager#setComponentEnabledSetting()禁用组件,这将具有将其从启动器中删除的作用。

    赞(0)
    未经允许不得转载: 京大飞辅助网程序员面试-区块链javago面经 » 程序员Android JNI中的本机崩溃SIGSEGV – java

    相关推荐

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址