Unhandled Exception - Read Access Violation


I’m learning C++ through Juce and an audio application.
The code is pretty simple right now, few buttons to load, play and stop the audio file.
Also, some sliders to control gain and speed.
The application compiles and executes fine except while in debug mode where a nasty exception appears and I get totally lost.
The application is generated from Projucer in its latest release 6.0.1 and exported into Visual Studio Enterprise 2019 16.6.4.

The following exception is met while closing the application:

Unhandled exception thrown: read access violation.
this->input.object._Mypair._Myval2-> was 0xFFFFFFFFFFFFFFEF.

and this is the Call Stack:

    >	OtoDecksWin.exe!juce::ResamplingAudioSource::releaseResources() Line 76	C++
     	OtoDecksWin.exe!juce::AudioTransportSource::setSource(juce::PositionableAudioSource * newSource, int readAheadSize, juce::TimeSliceThread * readAheadThread, double sourceSampleRateToCorrectFor, int maxNumChannels) Line 106	C++
     	OtoDecksWin.exe!juce::AudioTransportSource::~AudioTransportSource() Line 33	C++
     	OtoDecksWin.exe!MainComponent::~MainComponent() Line 80	C++
     	[External Code]	
     	OtoDecksWin.exe!juce::Component::SafePointer<juce::Component>::deleteAndZero() Line 2166	C++
     	OtoDecksWin.exe!juce::ResizableWindow::clearContentComponent() Line 108	C++
     	OtoDecksWin.exe!juce::ResizableWindow::~ResizableWindow() Line 58	C++
     	OtoDecksWin.exe!juce::DocumentWindow::~DocumentWindow() Line 79	C++
     	[External Code]	
     	OtoDecksWin.exe!OtoDecksWinApplication::shutdown() Line 36	C++
     	OtoDecksWin.exe!juce::JUCEApplicationBase::shutdownApp() Line 328	C++
     	OtoDecksWin.exe!juce::JUCEApplicationBase::main() Line 266	C++
     	OtoDecksWin.exe!WinMain(HINSTANCE__ * __formal, HINSTANCE__ * __formal, char * __formal, int __formal) Line 105	C++
     	[External Code]	

Following the traceback, I can understand the problem appears in the destructor while calling shutdownAudio(). Due to my limited experience and knowledge about both the language and the IDE the only option I pursued is to comment out as much as I could until the code block raising the exception was isolated.

void MainComponent::loadURL(juce::URL audioURL)
    auto* reader = formatManager.createReaderFor(audioURL.createInputStream(false));
    if (reader != nullptr)
        std::unique_ptr<juce::AudioFormatReaderSource> newSource(new juce::AudioFormatReaderSource(reader, true));
        transportSource.setSource(newSource.get(), 0, nullptr, reader->sampleRate);

that is pretty standard though, there is plenty of examples alike.

    juce::AudioFormatManager formatManager;
    juce::AudioTransportSource transportSource;
    std::unique_ptr<juce::AudioFormatReaderSource> readerSource;

    juce::ResamplingAudioSource resampleSource{&transportSource, false, 2};

Amongst my attempts to clear that exception out, I can mention the use of delete in the destructor.

I’m lost, but I’d like to understand how to identify the unreleased resource and avoid the exception at all with a best practice coding technique.

Any help is appreciated, thanks

Looks like that is the line in your code that leads the exception. What does your destructor code look like?

there it is,

    //readerSource = nullptr;
    // This shuts down the audio device and clears the audio source.

Comments were just test and trial and the only line is shutdownAudio(). The block that always trigger the exception is the loadURL and at any load operation. I call prepareToPlay and releaseResources on the AudioTransportSource object in my overrided methods.

The exception is resolved while adding the following line in the destructor,