Issue with AudioTransportSource / file reader pointer on application shutdown


#1

I have attempted to modify the audio thumbnail tutorial to my application for a “quick and dirty” file-chooser reader solution. For some reason in my project, when i attempt to “x” out of the application, the program crashes, though only once a file has been loaded, played, etc. I have looked through the forum, and it appears that I likely have a dangling pointer left over from where the “AudioTransportSource” instance is set to the new source, as the closest item in the stack trace points to the AudioTransport Source’ releaseResources method. This all seems to be tied to the implementation of the “new readerSource” method, and have attempted to deallocate the readerSource in the component’s releaseResources() override, as I saw suggested in a different forum post; however it has been to little avail…


#2

Can you post some code for how you are reading the file and setting the transport source?


#3

Hello, this is the code that was lifted directly from the AudioThumbnail tutorial, though I moved it into a cpp file instead of keeping it in the header:

void MainComponent::openButtonClicked()
{
  FileChooser chooser ("Select a Wave file to play...",
                     File(),
                     "*.wav");

   if (chooser.browseForFileToOpen())
   {
     File file (chooser.getResult());
    
       if (auto *reader = afManager.createReaderFor (file))
       {
         std::unique_ptr<AudioFormatReaderSource> newSource (new AudioFormatReaderSource (reader, true));
         transportSource.setSource (newSource.get(), 0, nullptr, reader->sampleRate);
         playButton.setEnabled (true);
         thumbComp.setFile (file);
         readerSource.reset (newSource.release());
      }
     }
    }

Here is what I have within the getNextAudioBlock function (note that I am running the samples through an analysis function, though when I comment this out it does not really seem to resolve the problem):

void MainComponent::getNextAudioBlock (const 
   AudioSourceChannelInfo& bufferToFill)
{
   if (readerSource.get() == nullptr){
     bufferToFill.clearActiveBufferRegion();
     return;
  }

  transportSource.getNextAudioBlock(bufferToFill);

  for(int i= 0 ; i < bufferToFill.buffer->getNumChannels(); ++i){
     auto* inputChannel = bufferToFill.buffer->getReadPointer(0, bufferToFill.startSample);
        
     for(int samp = 0; samp < bufferToFill.numSamples; ++samp){
            process(inputChannel[samp]);
     }         
   }
}

#4

I am guessing, that this is a result of the destruction order. Class members are destructed in the reverse order they are created.
I assume your transportSource is deleted after the readerSource.

  • either make sure you define the readerSource member before the transportSource member
  • or in your destructor unset the source of your transportSource by calling transportSource.setSource (nullptr);
  • or enforce the destruction order in the destructor by calling reset() on your unique_ptrs in a specific order

But have a special look at the point, where the debugger is actually failing.

Hope that helps


#5

Thanks man! the transportSource.setSource(nullptr) solution worked like a charm. I’m still getting the hang of pointers and such, I guess its something that takes a little while to become intuitive. Thanks again!