App crashes when using AudioFormatManager

Hello! I’m having a problem with an ios project that uses the AudioFormatManager. When trying to create a SamplerSound, the application crashes with an error
“JUCE Message Thread (1): EXC_BAD_ACCESS (code=1, address=0x8)”
In debugger, I can see that my “reader” object is NULL
here is the part of my code where the error occurs

void SamplerpluginAudioProcessor::loadFile()
    ScopedPointer<AudioFormatReader> reader;
    juce::FileChooser chooser { "Pleace load a file" };
        File file = chooser.getResult();
        reader = mFormatManager.createReaderFor(file);
        BigInteger range;
        range.setRange(0, 128, true);
        SamplerSound *sampSound = new SamplerSound("", *reader, range, 0, 0.0, 0.0, 30.f);

everything works fine in the emulator, but when I try to install the application on a real device, I encounter this problem…
maybe someone knows in which direction to dig and what could be the problem?
I will be very grateful for any help!

You should always check the result of createReaderFor, and avoid using the returned pointer if it is null. This will prevent crashes.

The most likely failure is that the input file cannot be opened for some reason. You could try checking whether the file itself can be opened successfully, by calling auto stream = file.createInputStream();.
If this returns non-null, you can create a reader for the stream: auto reader = formatManager.createReaderFor (std::move (stream));

If the file still can’t be opened when using a filepath, you could try accessing the file via its URL instead. The following is untested, but should point you in the right direction:

auto stream = chooser.getURLResult().createInputStream (URL::InputStreamOptions (URL::ParameterHandling::inAddress));

if (stream == nullptr)
    return; // add error handling

auto reader = rawToUniquePtr (formatManager.createReaderFor (std::move (stream)));

if (reader == nullptr)
    return; // add error handling

sampler.addSound (new SamplerSound ("", *reader, range, 64, 0.0, 0.0, 30.0));
1 Like

Thanks a lot! Accessing the file via its URL is a right solution! Thank you

Has anyone encountered the problem “Application initializing document picker is missing the iCloud entitlement. Is set?” My app crashes with this error when I try to run it on my iPad. On iPhone and on simulators everything works fine

the error occurs when calling my method, which was already discussed in this topic above

here is the text from the console:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'Application initializing document picker is missing 
the iCloud entitlement. Is set?'
terminating with uncaught exception of type NSException

thanks if anyone knows what could be the reason

If you’re using the Projucer, make sure you have set the “iCloud Permissons” option to “Enabled” in the iOS exporter settings. This capabiility is required in order to use the native load/save browsers on iOS.

Thanks! I thought so, but I was hoping that it was something else, because iCloud Permissons are only available in a paid Apple developer account