AudioFormatWriter leak

Hello,
This part of code would work “before” (some time ago, I don’t know exactly):



const File strippedLastRecordedSound = UPIAssetManager::getUserRecordedSounds().getChildFile (lastRecordedSound.getFileNameWithoutExtension()+"tmpStripped.wav");
if (auto fos = strippedLastRecordedSound.createOutputStream())
			{
				if (bufferToStrip.getNumSamples() > 0)
				{
					WavAudioFormat wavFormat;
                    std::unique_ptr <AudioFormatWriter> afw (wavFormat.createWriterFor(fos.get(), sampleRate, bufferToStrip.getNumChannels(), 16, StringPairArray(), 0));
					afw->writeFromAudioSampleBuffer (bufferToStrip, 0, bufferToStrip.getNumSamples());
					afw.reset();
				}
			}


Now, replacing afw.reset() by afw.release() goes without crashing, but may provoke a leak?

(In both cases, afw is written on disk. )

Replacing reset with release will leak, so this is not advised.

The docs for createWriterFor say this:

@param streamToWriteTo

the stream that the data will go to - this will be
deleted by the AudioFormatWriter object when it's no longer
needed. If no AudioFormatWriter can be created by this method,
the stream will NOT be deleted, so that the caller can re-use it
to try to open a different format, etc

This means if afw is not nullptr after the call to createWriterFor, then you should call fos.release() immediately in order to avoid the stream being deleted twice, first by the destructor of the AudioFormatWriter and then by the destructor of the unique_ptr<FileOutputStream>. There is no reason to call afw.reset() in the location shown in your snippet, as afw will be destroyed at the following } anyway.