AudioFormatWriter leak

If you look at the AudioRecordingDemo.h, it shows how to write a file using the WavAudioFormat.

In short:

File file; // destination file

if (auto fileStream = file.createOutputStream()) // heap-based stream
{
    if (auto writer = WavAudioFormat{}.createWriterFor (fileStream.get(), sampleRate, 1, 16, {}, 0))
    {
        fileStream.release(); // ownership has been passed to the writer

        writer->writeFromAudioSampleBuffer (...);
    }
}

As explained above, the input stream will be automatically deleted by the AudioFormatWriter if creating the writer is successful. This means you must use a heap-allocated output stream so that the stream can be deleted successfully later on. In your code, you’ve used a stack-allocated stream, and this will cause problems when the writer attempts to free the stream.

If your AudioFormatWriter is managed by a unique_ptr, there’s no need to reset or release it manually. Calling writer.release() prevents the unique_ptr from freeing the AudioFormatWriter at the end of the scope, causing the leak. I suggest removing this line.

1 Like