Getting error on AudioDeviceManager::initialise

I’m trying to test the AudioDeviceManager in Android but i cant make it to successfully call initialise or initialiseWithDefaultDevice(), here’s my code:

extern "C" {
static AudioDeviceManager *deviceManager;
JNIEXPORT void JNICALL
Java_com_solace_sol_MainActivityKt_playAudio(JNIEnv *env, jclass clazz, jstring filePath) {
    String err = deviceManager->initialiseWithDefaultDevices(0, 2);

    Log::e(err.toStdString().c_str());
}
}

It kept showing me this error:

Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 in tid 15998

You are not creating the AudioDeviceManager you are attempting to use.

You need to do something like :

static std::unique_ptr<AudioDeviceManager> deviceManager;
JNIEXPORT void JNICALL
Java_com_solace_sol_MainActivityKt_playAudio(JNIEnv *env, jclass clazz, jstring filePath) {
    if (!deviceManager) // is the devicemanager a null pointer?
    {     
      deviceManager = std::make_unique<juce::AudioDeviceManager>(); // if yes, then create and init the manager instance
      String err = deviceManager->initialiseWithDefaultDevices(0, 2);
      Log::e(err.toStdString().c_str());
    }
}
}

This would lead to a memory leak, though. You’d need another function exported to your Android side that cleans up your C++ object(s) after they are not needed anymore.

Maybe something like (I am not familiar with the Android interoperation, so the syntax for the function declaration might not be right) :

JNIEXPORT void JNICALL
Java_com_solace_sol_MainActivityKt_destroyAudioObjects(JNIEnv *env, jclass clazz)
{
  deviceManager = nullptr; 
}
1 Like

Thanks, i just fixed it and it worked