Issue Deleting FileInputStream && Reading Wav File


#1

Hello all,

I'm trying to extract some data from a .wav file using FileInputStream. Here is a code snippet of how I think this should be done with JUCE.


void FilterGraph::importFIRFilter(File filterFile)
{
    FileInputStream* fileInputStream = filterFile.createInputStream();
    ScopedPointer<AudioFormatReader> wavReader;
    WavAudioFormat wavAF;
    if(fileInputStream != nullptr){
        wavReader = wavAF.createReaderFor(fileInputStream, false);
        ScopedPointer<AudioSampleBuffer> buffer = new AudioSampleBuffer(2, FFT_SIZE);
        CFFT fft; 
    
        if(wavReader){
        
            wavReader->read(buffer,0,fftSize,0, true, true);
            float *data = buffer->getArrayOfChannels()[0];
        
            float outReal[FFT_SIZE], outIm[FFT_SIZE], outMag[FFT_SIZE/2];
            fft.fft_float(fftSize,false,data,NULL,outReal,outIm);      
        }
    }
    if(fileInputStream)
        delete fileInputStream;
}
    

VS throws an exception here:


AudioFormatReader::~AudioFormatReader()
{
    delete input;
}

By changing the fileInputStream to a ScopedPointer an exception is thrown here. I cannot determine what object is trying to be deleted, but something tells me it's the AudioFormatReader..


struct ContainerDeletePolicy
{
    static void destroy (ObjectType* object)
    {
        delete object;
    }
};

I have tried a few variations of the code but am unable to determine why this is happening! I'm still a student, so my experience is limited. Thanks for your help and C++ know-how.

 

UPDATE:

If I let the fileInputStream memory leak the program quits complaining and everything runs smoothly. This leads me to believe something is happening in the destructor that shouldn't. I think this answers my question, but I would like someone to confirm that I indeed do not need to call delete here as the memory has been managed elsewhere. I passed my pointer to AudioSampleBuffer which may have deleted it.


#2

The AudioFormatReader deletes the stream that you give it (it does explain this if you read its comments!)


#3

This.