audioFormatManager.createReaderFor breaks after upgrade to Juce 6


I was still using Juce 5 and now tried to upgrade to Juce 6. There’s one error that appears now:

auto* memoryInputStream = new MemoryInputStream(sound.getWav(), sound.getWavSize(), false);
auto* reader = audioFormatManager.createReaderFor(memoryInputStream); // this line breaks (line 144)
if (reader != nullptr)
{ ... }

The compiler says:
Error C2664 ‘juce::AudioFormatReader *juce::AudioFormatManager::createReaderFor(std::unique_ptr<ObjectType,std::default_delete>)’: cannot convert argument 1 from ‘juce::MemoryInputStream *’ to ‘const juce::File &’ line 144

What is your suggestion to fix this? It worked very well but seems to be deprecated now.

I use the reader to create SamplerSound instances. I’d prefer a solution that does not require a refactor of everything.

Have a nice weekend,

The code was improved. The former raw pointer didn’t convey the appropriate ownership.
Instead you should supply now a unique_ptr instead of the raw pointer:

auto memoryInputStream = std::make_unique<juce::MemoryInputStream>(sound.getWav(), sound.getWavSize(), false);
auto* reader = audioFormatManager.createReaderFor (std::move (memoryInputStream)); // the move is needed because ownership is transfered
if (reader != nullptr) // strangely reader is still a raw pointer, it might be refactored at some point, IDK.

Hope that helps

1 Like

Oh yes, thank you :blush: