Proper clean-up from playing a wave file


I need to play a wave file.  I have, more or less, harvested code snippets from the JUCE Demo solution to accomplish this first bit of work (see below), but I'm having problems with an exception being thrown when I exit.

Looking at the debugger it appears as though there is still an active WSAPI thread running, so I assume I haven't performed proper shutdown/clean-up on one of objects (maybe the reader object??).  Any ideas?

AudioFormatManager formatManager;

ScopedPointer<AudioDeviceManager> deviceManager = new AudioDeviceManager();
deviceManager->initialise (2, 2, nullptr, true, String::empty, nullptr);

AudioSourcePlayer audioSourcePlayer;

AudioTransportSource transportSource;
audioSourcePlayer.setSource (&transportSource);

ScopedPointer<AudioFormatReader> reader = formatManager.createReaderFor(srcFile);
ScopedPointer<AudioFormatReaderSource> currentAudioFileSource = 
    new AudioFormatReaderSource (reader, true);

transportSource.setSource (currentAudioFileSource, 0, nullptr, reader->sampleRate);

// in the future other will be done here
// this works until that process is hooked up

// stop and clean-up...
transportSource.setSource (nullptr);
audioSourcePlayer.setSource (nullptr);
deviceManager->removeAudioCallback (&audioSourcePlayer);

return; // exception thrown here....




It seems I have to remove the ScopedPointer off of the AudioFormatReader in order to stop getting the exception.

From this...

ScopedPointer<AudioFormatReader> reader = formatManager.createReaderFor(srcFile);

To this...

AudioFormatReader *reader = formatManager.createReaderFor(srcFile);

But who will free the reader?  According to the tooltip for createReaderFor (File&), it is the caller's responsiblity to free the reader.


 According to the tooltip for createReaderFor (File&), it is the caller's responsiblity to free the reader.

No - the docs for createReaderFor say the exact opposite!


From juce_AudioFormatManager.h...

    /** Searches through the known formats to try to create a suitable reader for
        this file.
        If none of the registered formats can open the file, it'll return 0. If it
        returns a reader, it's the caller's responsibility to delete the reader.
    AudioFormatReader* createReaderFor (const File& audioFile);

And it says the same thing in the online doc ere...

No big deal, I fixed my code.


Oh, sorry, I mis-read that, and thought you were asking about deleting the stream, not the reader.