[Beginner] Access violation in ~AudioTransportSource


I have written a simple audio player plugin, mostly following the https://www.juce.com/doc/tutorial_playing_sound_files tutorial. Everything is fine, except when I close the JUCE Plug-in host I get an

Exception thrown: read access violation.

this->input.object.object-> was 0xDDDDDDDD.

every time.
The stack trace looks like this:

NewProject.dll!juce::ResamplingAudioSource::releaseResources() Line 77 C++
NewProject.dll!juce::AudioTransportSource::setSource(juce::PositionableAudioSource * newSource, int readAheadSize, juce::TimeSliceThread * readAheadThread, double sourceSampleRateToCorrectFor, int maxNumChannels) Line 121 C++
NewProject.dll!juce::AudioTransportSource::~AudioTransportSource() Line 47 C++
NewProject.dll!NewProjectAudioProcessor::~NewProjectAudioProcessor() Line 24 C++

The relevant code boils down to this:

void NewProjectAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
// Use this method as the place to do any pre-playback
// initialisation that you need…
transportSource.prepareToPlay(samplesPerBlock, sampleRate);

var filePath = “path to some .wav file”;
auto file = File(filePath);

AudioFormatReader* reader = formatManager.createReaderFor(file);
if (reader != nullptr)
ScopedPointer newSource = new AudioFormatReaderSource(reader, true);
transportSource.setSource(newSource, 0, nullptr, reader->sampleRate);
readerSource = newSource.release();

void NewProjectAudioProcessor::releaseResources()
// When playback stops, you can use this as an opportunity to free up any
// spare memory, etc.

Which is pretty much copy paste from the tutorial. I am a C++ noob and I just don’t see, what the problem is here. I am sure that I have missed something trivial, so I am sorry for a stupid question :slight_smile:
Thanks for reading :slight_smile:


Hi Wire,

Did you try the tutorial in a regular application first?

It looks like it’s crashing in your destructor, what’s happening in there or is it just empty?

BTW this seems related to this post:


thanks for your reply. In the regular app it works just fine.

My destructor in the example I have given is completely empty.

Anyway, I have managed to fix it, but I still don’t really understand the issue.

I needed to change the readerSource from ScopedPointer to a regular pointer, change the transportSource also to a pointer, too. Then, in the destructor, If I first delete the transportSource and then the readerSource, it does not crash. :slight_smile:


N.B. you can also enforce the sequence of the destruction of ScopedPointers by explicitly setting them to nullptr.

transportSource = nullptr;  // calls the destructor of transportSource, given it is a ScopedPointer