Hello,
I'm testing the Playing Sound Files tutorial on Visual Studio 2015. It's running on the debugger. When loading the first file it loads and play it fine. But right after opening a second file the program raises an "Access Violation" exception at ResamplingAudioSource::releaseResources().
The call stack points to the call transportSource.setSource in MainComponent::openButtonClicked:
void openButtonClicked()
{
FileChooser chooser ("Select a Wave file to play...",
File::nonexistent,
"*.wav"); // [7]
if (chooser.browseForFileToOpen()) // [8]
{
File file (chooser.getResult()); // [9]
AudioFormatReader* reader = formatManager.createReaderFor (file); // [10]
if (reader != nullptr)
{
readerSource = new AudioFormatReaderSource (reader, true); // [11]
transportSource.setSource (readerSource, 0, nullptr, reader->sampleRate); // [12]
playButton.setEnabled (true); // [13]
}
}
}
The code above allocates readerSource with the new keyword. This code runs fine the first time, but the second time raises the described exception.
I've been able to fix the problem by releasing readerSource using the delete keyword before the new allocation:
void openButtonClicked()
{
FileChooser chooser ("Select a Wave file to play...",
File::nonexistent,
"*.wav");
if (chooser.browseForFileToOpen())
{
File file (chooser.getResult());
AudioFormatReader* reader = formatManager.createReaderFor (file);
if (reader != nullptr)
{
if (readerSource != nullptr) delete readerSource;
readerSource = new AudioFormatReaderSource (reader, true);
transportSource.setSource (readerSource, 0, nullptr, reader->sampleRate);
playButton.setEnabled (true);
}
}
}
The above code works correctly without issues.
I've noticed that readerSource is declared as:
ScopedPointer<AudioFormatReaderSource> readerSource
The JUCE Coding Standards discourage the use of the new and delete keywords. The ScopedPointer template automatically deletes the pointer when the object goes out of scope, but looks like it doesn't gets properly released in this case.
My questions are:
- Which is the correct way for reusing a pointer like in the above case? ScopedPointer + new raises an exception when the pointer is passed as argument to a method.
- Is my solution using new / delete valid? Is there a better solution?
Thank you very much!
Edy
