From 8ba09acc594b49cb131d3ad6ce71b8f920fe7bac Mon Sep 17 00:00:00 2001 From: reuk Date: Thu, 10 Mar 2022 20:40:03 +0000 Subject: [PATCH] VST2 Client: Avoid potential deadlocks on stateInformationLock --- .../VST/juce_VST_Wrapper.cpp | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/modules/juce_audio_plugin_client/VST/juce_VST_Wrapper.cpp b/modules/juce_audio_plugin_client/VST/juce_VST_Wrapper.cpp index adf53e4528..d0842eb5c2 100644 --- a/modules/juce_audio_plugin_client/VST/juce_VST_Wrapper.cpp +++ b/modules/juce_audio_plugin_client/VST/juce_VST_Wrapper.cpp @@ -1720,14 +1720,17 @@ private: auto data = (void**) args.ptr; bool onlyStoreCurrentProgramData = (args.index != 0); - ScopedLock lock (stateInformationLock); - chunkMemory.reset(); + MemoryBlock block; if (onlyStoreCurrentProgramData) - processor->getCurrentProgramStateInformation (chunkMemory); + processor->getCurrentProgramStateInformation (block); else - processor->getStateInformation (chunkMemory); + processor->getStateInformation (block); + // IMPORTANT! Don't call getStateInfo while holding this lock! + const ScopedLock lock (stateInformationLock); + + chunkMemory = std::move (block); *data = (void*) chunkMemory.getData(); // because the chunk is only needed temporarily by the host (or at least you'd @@ -1746,18 +1749,18 @@ private: bool onlyRestoreCurrentProgramData = (args.index != 0); { - ScopedLock lock (stateInformationLock); + const ScopedLock lock (stateInformationLock); chunkMemory.reset(); chunkMemoryTime = 0; + } - if (byteSize > 0 && data != nullptr) - { - if (onlyRestoreCurrentProgramData) - processor->setCurrentProgramStateInformation (data, byteSize); - else - processor->setStateInformation (data, byteSize); - } + if (byteSize > 0 && data != nullptr) + { + if (onlyRestoreCurrentProgramData) + processor->setCurrentProgramStateInformation (data, byteSize); + else + processor->setStateInformation (data, byteSize); } }