From 1e902bcdeed01ac715ba2fa5523291a575117e60 Mon Sep 17 00:00:00 2001 From: reuk Date: Thu, 10 Oct 2024 14:05:08 +0100 Subject: [PATCH] VST3: Avoid potential memory allocations during buffer remapping --- .../format_types/juce_VST3Common.h | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/modules/juce_audio_processors/format_types/juce_VST3Common.h b/modules/juce_audio_processors/format_types/juce_VST3Common.h index 5a8a089dc0..951977e80d 100644 --- a/modules/juce_audio_processors/format_types/juce_VST3Common.h +++ b/modules/juce_audio_processors/format_types/juce_VST3Common.h @@ -1119,9 +1119,17 @@ public: { mappings = std::move (arrangements); - floatBusMap .resize (mappings.size()); - doubleBusMap.resize (mappings.size()); - busBuffers .resize (mappings.size()); + const auto numBuses = mappings.size(); + floatBusMap .resize (numBuses); + doubleBusMap.resize (numBuses); + busBuffers .resize (numBuses); + + for (auto [index, busMap] : enumerate (mappings, size_t{})) + { + const auto numChannels = busMap.size(); + floatBusMap [index].reserve (numChannels); + doubleBusMap[index].reserve (numChannels); + } } /* Applies the mapping to an AudioBuffer using JUCE channel layout. */ @@ -1157,6 +1165,10 @@ private: const ChannelMapping& busMap, int channelStartOffset) const { + // If this is hit, we may be about to allocate memory. Ideally, this should have been + // allocated in prepare(). + jassert (busMap.size() <= bus.capacity()); + bus.clear(); for (size_t i = 0; i < busMap.size(); ++i)