MidiInput::openDevice and MidiOutput::openDevice errors

Hi,
I am new to Juce 5. Few years back I used Juce 3. Now I am running into errors with couple of simple functions, I wonder what I might be missing?

midiInput = MidiInput::openDevice(0, this);

and

midiOutput = MidiOutput::openDevice(0);

VS 2019 error:

Severity Code Description Project File Line Suppression State
Error C2440 ‘=’: cannot convert from ‘std::unique_ptr<juce::MidiOutput,std::default_deletejuce::MidiOutput>’ to ‘juce::MidiOutput *’

and

Severity Code Description Project File Line Suppression State
Error C2440 ‘=’: cannot convert from ‘std::unique_ptr<juce::MidiInput,std::default_deletejuce::MidiInput>’ to ‘juce::MidiInput *’

The openDevice functions were changed to return a std::unique_ptr instead of a raw pointer in this commit:

Thank you so much for your time. I have to redesign completely some MIDI functions and now I am stack with the following.

If I try to startBackgroundThread then I get this unhandled exception bad access error: void CriticalSection::enter() const noexcept { EnterCriticalSection ((CRITICAL_SECTION*) lock); }

	MidiOutput* midi_Output;

        jassert (midiOutputs[index]->outDevice.get() == nullptr);
        midiOutputs[index]->outDevice.reset (MidiOutput::openDevice (midiOutputs[index]->deviceInfo.identifier));
       
	//midiOutputs[index]->outDevice = MidiOutput::openDevice (midiOutputs[index]->deviceInfo.identifier);
	  midi_Output->openDevice(midiOutputs[index]->deviceInfo.identifier);

       //if (midiOutputs[index]->outDevice.get() == nullptr)
       if (midi_Output != nullptr)
	{

		midi_Output->startBackgroundThread(); // this creates bad access error
					
	}

I’m afraid you need to look up how unique_ptr works.
A unique_ptr is a little wrapper object, moveable but not copyable, usually living on the stack or in a container. Its purpose is to own an object (pointee) and delete it automatically, once that unique_ptr goes out of scope.

midi_Output seems to be undefined anyway.

Thank you. You are right. What was simple in JUCE 3 it got now a little more complicated.
I ended up attaching it directly like this:

midiOutputs[index]->outDevice->startBackgroundThread();

It seems to be working… Probably there are more road blocks ahead while converting from Juce3.
Thank you for your help!

I can relate how annoying it is to update code that was working fine. But the stability improvements will be worth it :slight_smile:

You can get a reference to the unique_ptr and use it as normal:

auto& midi_Output = midiOutputs[index];
if (midi_Output)
    midi_Output->startBackgroundThread();