Writing a WAV file from an AudioSampleBuff, unexpected error

File ofile(file); FileOutputStream* outputTo = ofile.createOutputStream(); WavAudioFormat* format = new WavAudioFormat(); AudioFormatWriter* writer = format->createWriterFor(outputTo, fs, 1, nbits, NULL, 0); wave->writeToAudioWriter(writer,0,wave->getNumSamples());

delete writer;
I get a very strange (and frustrating) error at the end of the first block (writeToAudioWriter) when it hits line 667 of juce_AudioSampleBuffer.cpp

if (writer->isFloatingPoint())
Access violation reading location 0x00000018.

Its gotta be something stupid but I can’t find it. Anybody can give me a hint? Thanks!

Looks like a classic dangling pointer - you’ll have deleted ‘writer’ before you try to call a method on it.

Well, it’s done for a good reason - say you write a function that creates a writer for something, and returns it (creating some kind of input stream internally). The way it is now, you just get that writer, use it, pass it around, and delete it when you’re done, letting it clean up its own input stream. But if the stream was separate, you’d need to manage both the writer and its stream.