sendMessageNow not working all of a sudden

I’m making an audio effect, specifically for use in Logic Pro on mac, to send MIDI CC messages. This code was working yesterday, and isn’t working now all of a sudden, which makes me worry that I’ve been doing something undefined.

Here are the relavant parts of the code:

void EnvelopeFollowerAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
{
    // ... Code omitted for clarity ...
    juce::String id = juce::MidiOutput::getDefaultDevice().identifier;
    output_device = juce::MidiOutput::openDevice(id);
    if (!output_device) {
        std::cout<<"Unable to create output device\n";
    }
}
void EnvelopeFollowerAudioProcessor::processBlock (juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages)
{
    // ... Code omitted for clarity ...
            sendCCMessage(index);
    // ... Code omitted for clarity ...
}
void EnvelopeFollowerAudioProcessor::sendCCMessage(int sample_number)
{
    juce::MidiMessage message;
    message = juce::MidiMessage::controllerEvent(midi_channel, midi_controller_type, midi_value);
    output_device->sendMessageNow(message);
}

Logic is not receiving the MIDI CC messages, but it is receiving CC messages sent to the IAC driver bus from other applications. I noticed often people used threading/locking for parts of the MIDI sending process, could that be the problem?

In general dispatching messages to MIDI devices cannot be considered thread-safe, so this is something you will want to avoid doing on the audio thread.

You can watch Fabian and Dave’s ADC talks to give you some strategies for dealing with this: