AudioDeviceIOCallback Call Jni method get random error

Here is my audioDeviceIOCallback code

void AudioIOCallbackImpl::audioDeviceIOCallback(const float **inputChannelData,
                                            int numInputChannels, float **outputChannelData,
                                            int numOutputChannels, int numSamples) {
const ScopedWriteLock writelock(lock);
Logger::outputDebugString(
        "audioDeviceIOCallback: numInputChannels = " + String(numInputChannels) +
        ", numSample = " + String(numSamples));
//    ostringstream str;
//    for (int i = 0; i < numInputChannels; i++) {
//        for (int j = 0; j < numSamples; j++) {
//            str << inputChannelData[i][j] << ", ";
//        }
//    }
if (numInputChannels < 1) {
    return;
}

int64 now = Time::currentTimeMillis();
Logger::outputDebugString("audioDeviceIOCallback time cost:" + String(now - time));
time = now;
JNIEnv *env;
if (javaVM == nullptr) {
    Logger::outputDebugString("JavaVM Null");
    return;
}
javaVM->AttachCurrentThread(&env, nullptr);
if (env == nullptr) {
    Logger::outputDebugString("JNIEnv Null");
    return;
}

jfloatArray fArr = env->NewFloatArray(numSamples);
env->SetFloatArrayRegion(fArr, 0, numSamples, inputChannelData[0]);
jclass class_JuceJNIHelper = env->FindClass("org/cocos2dx/cpp/JuceJNIHelper");
if (class_JuceJNIHelper == nullptr) {
    Logger::outputDebugString("class_JuceJNIHelper Null");
    return;
}
jmethodID method_JuceJNIHelper_reciveRecordData = env->GetStaticMethodID(class_JuceJNIHelper,
                                                                         "reciveRecordData",
                                                                         "([FJ)V");
if (method_JuceJNIHelper_reciveRecordData == nullptr) {
    Logger::outputDebugString("method_JuceJNIHelper_reciveRecordData Null");
    env->DeleteLocalRef(fArr);
    return;
}
env->CallStaticVoidMethod(class_JuceJNIHelper, method_JuceJNIHelper_reciveRecordData, fArr,
                          now - timeDelay);
env->DeleteLocalRef(fArr);

}

And I got Ndk-Stack Crash Dump:

********** Crash dump: **********
Build fingerprint: 'Huawei/C8818/hwC8818:4.4.4/HuaweiC8818/C92B203:user/ota-rel-keys,release-keys'
pid: 20518, tid: 20735, name: OpenSL  >>> com.blw.guitar2dx <<<
signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
Stack frame #00  pc 0002236c  /system/lib/libc.so (tgkill+12)
Stack frame #01  pc 000133c1  /system/lib/libc.so (pthread_kill+48)
Stack frame #02  pc 000135d5  /system/lib/libc.so (raise+10)
Stack frame #03  pc 0001230b  /system/lib/libc.so
Stack frame #04  pc 00021c20  /system/lib/libc.so (abort+4)
Stack frame #05  pc 00048e0f  /system/lib/libdvm.so (dvmAbort+78)
Stack frame #06  pc 0004c2db  /system/lib/libdvm.so
Stack frame #07  pc 000511d5  /system/lib/libdvm.so (dvmCallJNIMethod(unsigned int const*, JValue*, Method const*, Thread*)+124)
Stack frame #08  pc 00029aa0  /system/lib/libdvm.so
Stack frame #09  pc 00030f5c  /system/lib/libdvm.so (dvmMterpStd(Thread*)+76)
Stack frame #10  pc 0002e5f4  /system/lib/libdvm.so (dvmInterpret(Thread*, Method const*, JValue*)+184)
Stack frame #11  pc 00063761  /system/lib/libdvm.so (dvmCallMethodV(Thread*, Method const*, Object*, bool, JValue*, std::__va_list)+336)
Stack frame #12  pc 0004cea7  /system/lib/libdvm.so
Stack frame #13  pc 004e4e7f  /data/app-lib/com.blw.guitar2dx-2/libjuce_jni.so (_JNIEnv::CallStaticVoidMethod(_jclass*, _jmethodID*, ...)+34): Routine _JNIEnv::CallStaticVoidMethod(_jclass*, _jmethodID*, ...) at /Volumes/MAC/android-ndk-r10e/platforms/android-15/arch-arm/usr/include/jni.h:807
Stack frame #14  pc 004e5167  /data/app-lib/com.blw.guitar2dx-2/libjuce_jni.so (AudioIOCallbackImpl::audioDeviceIOCallback(float const**, int, float**, int, int)+610): Routine AudioIOCallbackImpl::audioDeviceIOCallback(float const**, int, float**, int, int) at /Volumes/MAC/project/JuceAudioTestProj/Builds/AndroidStudio/app/src/main/jni/Source/AudioIOCallbackImpl.cpp:54
Stack frame #00  pc 0002236c  /system/lib/libc.so (tgkill+12) }{stackFuzzy, #00  pc 0002236c  /system/lib/libc.so (tgkill+12),#01  pc 000133c1  /system/lib/libc.so (pthread_kill+48),#02  pc 000135d5  /system/lib/libc.so (raise+10),#03  pc 0001230b  /system/lib/libc.so,#04  pc 00021c20  /system/lib/libc.so (abort+4),#05  pc 00048e0f  /system/lib/libdvm.so (dvmAbort+78),#06  pc 0004c2db  /system/lib/libdvm.so,#07  pc 000511d5  /system/lib/libdvm.so (dvmCallJNIMethod(unsigned int const*, JValue*, Method const*, Thread*)+124),#08  pc 00029aa0  /system/lib/libdvm.so,#09  pc 00030f5c  /system/lib/libdvm.so (dvmMterpStd(Thread*)+76),#10  pc 0002e5f4  /system/lib/libdvm.so (dvmInterpret(Thread*, Method const*, JValue*)+184),#11  pc 00063761  /system/lib/libdvm.so (dvmCallMethodV(Thread*, Method const*, Object*, bool, JValue*, std::__va_list)+336),#12  pc 0004cea7  /system/lib/libdvm.so,#13  pc 004e4e7f  /data/app-lib/com.blw.guitar2dx-2/libjuce_jni.so (_JNIEnv::CallStaticVoidMethod(_jclass*, _jmethodID*, ...)+34),#14  pc 004e5167  /data/app-lib/com.blw.guitar2dx-2/libjuce_jni.so (AudioIOCallbackImpl::audioDeviceIOCallback(float const**, int, float**, int, int)+610) }
Stack frame #00  pc 0002236c  /system/lib/libc.so (tgkill+12);#01  pc 000133c1  /system/lib/libc.so (pthread_kill+48);#02  pc 000135d5  /system/lib/libc.so (raise+10);#03  pc 0001230b  /system/lib/libc.so;#04  pc 00021c20  /system/lib/libc.so (abort+4);#05  pc 00048e0f  /system/lib/libdvm.so (dvmAbort+78);#06  pc 0004c2db  /system/lib/libdvm.so;#07  pc 000511d5  /system/lib/libdvm.so (dvmCallJNIMethod(unsigned int const*, JValue*, Method const*, Thread*)+124);#08  pc 00029aa0  /system/lib/libdvm.so;#09  pc 00030f5c  /system/lib/libdvm.so (dvmMterpStd(Thread*)+76);#10  pc 0002e5f4  /system/lib/libdvm.so (dvmInterpret(Thread*, Method const*, JValue*)+184);#11  pc 00063761  /system/lib/libdvm.so (dvmCallMethodV(Thread*, Method const*, Object*, bool, JValue*, std::__va_list)+336);#12  pc 0004cea7  /system/lib/libdvm.so;#13  pc 004e4e7f  /data/app-lib/com.blw.guitar2dx-2/libjuce_jni.so (_JNIEnv::CallStaticVoidMethod(_jclass*, _jmethodID*, ...)+34);#14  pc 004e5167  /data/app-lib/com.blw.guitar2dx-2/libjuce_jni.so (AudioIOCallbackImpl::audioDeviceIOCallback(float const**, int, float**, int, int)+610); version_number=null module_name=com.blw.guitar2dx time_stamp=20160510183034 size=0 test_mode=NORMAL

The error line code is:

env->CallStaticVoidMethod(class_JuceJNIHelper, method_JuceJNIHelper_reciveRecordData, fArr,
now - timeDelay);

I guess it’s maybe a multi thread error.but I don’t know how to solve this problem.
It’s not got this error every time after callback audioDeviceIOCallback.
Only got this error after I run some time,maybe 1min or longer.
Please help me,is it a thread problem or release memory problem?

You seem to just have a load of mysterious java junk in your callback. What were you expecting to happen in there?

I’m just receive the Mic data,and send it to Java through jni…
But it’s alaways crashed after I run some time…like I said…maybe 1 min or longer time.
It must be crashed by the code:

env->CallStaticVoidMethod(class_JuceJNIHelper, method_JuceJNIHelper_reciveRecordData, fArr,
                          now - timeDelay);

Making a java call inside an audio callback is probably just about the worst thing you could possibly do in there.

haha,what a big mistake

then what you suggest me to pass the audio data to Java?

Most of my project’s code is written by Java.
I don’t want to change too much.and I’m not good at cpp.
because of the audio delay,I have to use Juce Audio Module instead of Android’s AudioRecord Class.
(English is not my native language,I hope I express my self clear)

java is very bad for anything realtime. You can use JNI but not from the audio thread!