Programmatically changing input/output audio device doesn't always work

I’ve been working on a project in JUCE (Audio app using AudioAppComponent) for a while and I’ve stumbled upon a problem that I can’t understand.
The app uses custom input/output audio devices (myInputDevice and myOutputDevice) made using Device Aggregation (through Audio MIDI Setup) on MacOSx; both input and output audio devices have 4 channels.
Neither of these devices is used a system audio device, as I want to keep using my headphones for that.

For this reason, in the constructor of my AudioAppComponent object (named MainComponent), I included some code I took from the JUCE DemoRunner demo about Audio Settings (AudioSettingsDemo.h). This code should programmatically set the audio device used in the AudioAppComponent to my custom input/output devices in place of the system audio device (i.e. my headphones).

The code below, showing both a snippet of the h and the cpp files works most times but sometimes yields an error at the following jassert:

const Type* getReadPointer (int channelNumber, int sampleIndex) const noexcept
    jassert (isPositiveAndBelow (channelNumber, numChannels));

I tried to trace back the reason of this and it seems to be related to the fact that when calling the setAudioChannels() function, the system device is used in place of my custom devices and the former doesn’t have 4 channels, so that when the copyFrom method within getNextAudioBlock() is called, the error occurs.
However, this doesn’t happen all the time, which makes it quite a nuisance to debug…

Now I must admit that I don’t understand some of the details going on in the code, e.g. I tried to call setAudioChannels() after actually setting up the custom audio device using setAudioDeviceSetup but that doesn’t get me any audio in or out the app.
Hopefully, some of you can see what I’m doing wrong.

class MainComponent   : public AudioAppComponent
    // --------
    void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
    void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override;
    void releaseResources() override;


    //  ------ audioDeviceArea
    AudioDeviceManager::AudioDeviceSetup currentAudioSetup;
    AudioDeviceSelectorComponent audioSetupComp;


MainComponent::MainComponent() :
                 audioSetupComp (deviceManager,
                      0,     // minimum input channels
                      4,   // maximum input channels
                      0,     // minimum output channels
                      4,   // maximum output channels
                      false, // ability to select midi inputs
                      false, // ability to select midi output device
                      false, // treat channels as stereo pairs
                      false), // hide advanced options
    // --------------------------------
    // Setup device manager

    // I need to make sure that IN/OUT have (or can have) the same sampling freq!!!!
    currentAudioSetup.inputDeviceName = "myInputDevice";
    currentAudioSetup.outputDeviceName = "myOutputDevice";
    //    Define IN/OUT channels
    numChannelIN = 4;
    numChannelOUT = 4;
    // Set audio device (I/O) buffer size and fs
    currentAudioSetup.bufferSize = currBuffSize;
    currentAudioSetup.sampleRate = currFs;

    // Some platforms require permissions to open input channels so request that here
    if (juce::RuntimePermissions::isRequired (juce::RuntimePermissions::recordAudio)
       && ! juce::RuntimePermissions::isGranted (juce::RuntimePermissions::recordAudio))
       juce::RuntimePermissions::request (juce::RuntimePermissions::recordAudio,
                                          [&] (bool granted) { setAudioChannels (granted ? 2 : 0, 2); });
       // Specify the number of input and output channels that we want to open
       setAudioChannels (numChannelIN, numChannelOUT);

    // Use custom bufferSize and sampleRate [ must come after setAudioChannels() ]
    deviceManager.setAudioDeviceSetup(currentAudioSetup, true);
    // --------------------------------

void MainComponent::getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill)
    // Get audio from input 4 
    myBuffer.copyFrom(0, 0, bufferToFill.buffer->getReadPointer( 3, bufferToFill.startSample), currBuffSize);