From f160c67f9f8f24649f9d8d477d81b23f2fbbd775 Mon Sep 17 00:00:00 2001 From: James H Ball Date: Wed, 21 Aug 2024 11:11:48 +0100 Subject: [PATCH] Prevent setting toggle state after object deletion --- Source/components/EffectsListComponent.cpp | 4 ++++ Source/components/SwitchButton.h | 9 +++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Source/components/EffectsListComponent.cpp b/Source/components/EffectsListComponent.cpp index bfa1e5d..6be859e 100644 --- a/Source/components/EffectsListComponent.cpp +++ b/Source/components/EffectsListComponent.cpp @@ -5,6 +5,10 @@ EffectsListComponent::EffectsListComponent(DraggableListBox& lb, AudioEffectListBoxItemData& data, int rn, Effect& effect) : DraggableListBoxItem(lb, data, rn), effect(effect), audioProcessor(data.audioProcessor), editor(data.editor) { + if (effect.enabled == nullptr) { + DBG("Effect enabled is null"); + } + auto parameters = effect.parameters; for (int i = 0; i < parameters.size(); i++) { std::shared_ptr effectComponent = std::make_shared(audioProcessor, effect, i); diff --git a/Source/components/SwitchButton.h b/Source/components/SwitchButton.h index 3b66b83..b3743e3 100644 --- a/Source/components/SwitchButton.h +++ b/Source/components/SwitchButton.h @@ -77,8 +77,11 @@ public: } void parameterValueChanged(int parameterIndex, float newValue) override { - juce::MessageManager::callAsync([this]() { - setToggleState(parameter->getBoolValue(), juce::NotificationType::dontSendNotification); + juce::WeakReference weakThis = this; + juce::MessageManager::callAsync([weakThis, this]() { + if (weakThis != nullptr) { + setToggleState(parameter->getBoolValue(), juce::NotificationType::dontSendNotification); + } }); } @@ -174,6 +177,8 @@ private: bool prevToggleState = false; BooleanParameter* parameter = nullptr; + + JUCE_DECLARE_WEAK_REFERENCEABLE(SwitchButton) }; } // namespace jux