From 7984458a8e1337ea5200a6fd72f1418c8079c0b5 Mon Sep 17 00:00:00 2001 From: reuk Date: Tue, 7 Dec 2021 13:28:50 +0000 Subject: [PATCH] InAppPurchasesDemo: Avoid heap-use-after-free when rapidly playing samples --- examples/Utilities/InAppPurchasesDemo.h | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/examples/Utilities/InAppPurchasesDemo.h b/examples/Utilities/InAppPurchasesDemo.h index af0491be2b..b9157457fc 100644 --- a/examples/Utilities/InAppPurchasesDemo.h +++ b/examples/Utilities/InAppPurchasesDemo.h @@ -480,6 +480,8 @@ class InAppPurchasesDemo : public Component, public: InAppPurchasesDemo() { + manager.registerBasicFormats(); + Desktop::getInstance().getDefaultLookAndFeel().setUsingNativeAlertWindows (true); dm.addAudioCallback (&player); @@ -568,12 +570,8 @@ private: auto assetName = "Purchases/" + soundNames[idx] + String (phraseListBox.getSelectedRow()) + ".ogg"; if (auto fileStream = createAssetInputStream (assetName.toRawUTF8())) - { - currentPhraseData.reset(); - fileStream->readIntoMemoryBlock (currentPhraseData); - - player.play (currentPhraseData.getData(), currentPhraseData.getSize()); - } + if (auto* reader = manager.createReaderFor (std::move (fileStream))) + player.play (reader, true); } } @@ -593,7 +591,7 @@ private: ListBox voiceListBox { "voiceListBox" }; std::unique_ptr voiceModel { new VoiceModel (purchases) }; - MemoryBlock currentPhraseData; + AudioFormatManager manager; JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InAppPurchasesDemo) };