AudioFormatWriter and wav file writing error


#1

Im building a small aplication to write noise in a wav file, here is the code:

 


    int size=500000;
    ScopedPointer<AudioSampleBuffer> buffer=new AudioSampleBuffer(2,size);
    buffer->clear();
    Random number(Time::currentTimeMillis());
    
    for(int i=0;i<size;i++)
        {
            float n=number.nextFloat();
            buffer->setSample(0,i,n);
            buffer->setSample(1,i,n);
        }
    ScopedPointer<WavAudioFormat> test = new WavAudioFormat();
    File outputFile = File("C:\\SDKS\\Projects\\test.wav"); 
    ScopedPointer<FileOutputStream> outputTo = outputFile.createOutputStream();
    
    ScopedPointer<AudioFormatWriter> writer = test->createWriterFor(outputTo, 44100, 2, 16,NULL, 0);
    writer->writeFromAudioSampleBuffer(*buffer,0,buffer->getNumSamples());
    //Not sure how to free memory
    writer=nullptr;

I'm a beginner(please be patient),  I'm getting this Unhandled exception at 0x00E5245D in Wav File.exe: 0xC0000005: Access violation reading location 0x00000000. in the debbuger please help.


#2

The exception shows that you are derefencing a nullptr. This can happen if any of your function fails for some reason (for example, if it can't open the file). You should be checking the results of createOutputStream and createWriterFor if they are returning null pointers. Usually it's easiest to step through your program with a debugger to get more detailed error information (for example, exactly what is causing the nullptr exception)