MidiOutput memory leakage


Hello everyone!

My audio app runs fine and throws an exception when it closes. I am having memory leakage and I know I need to use std::unique_ptr to stop this but this is not working for the MidiOutput class. I have declared my object as follows:

MidiOutput *midiOutput = MidiOutput::openDevice(0);

how can I do this using std::unique_ptr or something similar.


std::unique_ptr<MidiOutput> moutput{ MidiOutput::openDevice(0) };

But you might want to avoid initializing the device right in your class members. There’s obviously no guarantee the device number 0 will always work. (The unique_ptr would end up containing a null pointer in that case, though, so you can still check that later, though…)

1 Like


Also you don’t need to set the unique_ptr immediately. It is perfectly fine to use the default constructor, that will point to nullptr.

std::unique_ptr<MidiOutput> moutput;

// later
if (moutput == nullptr && MidiOutput::getDevices().size() > 0)
    moutput.reset (MidiOutput::openDevice (MidiOutput::getDefaultDeviceIndex()));

You still should test each time you try to use the device, like Xenakios said correctly, if there was no MidiOutput available, it can still be a nullptr…

1 Like


This works! Thanks so much for the fast response!