From 35890478a3bce506ebcc402d7b290af5be6bf880 Mon Sep 17 00:00:00 2001 From: ed Date: Fri, 16 Nov 2018 15:53:29 +0000 Subject: [PATCH] Added the JUCE_DECLARE_WEAK_REFERENCEABLE macro to ValueWithDefault and fixed some places which were potentially accessing a deleted ValueWithDefault object --- .../values/juce_ValueWithDefault.h | 14 +++++++++++++- .../properties/juce_ChoicePropertyComponent.cpp | 8 +++++++- .../properties/juce_ChoicePropertyComponent.h | 3 +-- .../juce_MultiChoicePropertyComponent.cpp | 8 +++++++- .../properties/juce_MultiChoicePropertyComponent.h | 3 +-- .../properties/juce_TextPropertyComponent.cpp | 10 ++++++++-- .../properties/juce_TextPropertyComponent.h | 3 +-- 7 files changed, 38 insertions(+), 11 deletions(-) diff --git a/modules/juce_data_structures/values/juce_ValueWithDefault.h b/modules/juce_data_structures/values/juce_ValueWithDefault.h index d30dfc039e..bb056f8641 100644 --- a/modules/juce_data_structures/values/juce_ValueWithDefault.h +++ b/modules/juce_data_structures/values/juce_ValueWithDefault.h @@ -181,6 +181,15 @@ public: /** Returns the property ID of the referenced property. */ Identifier& getPropertyID() noexcept { return targetProperty; } + //============================================================================== + ValueWithDefault& operator= (const ValueWithDefault& other) + { + referToWithDefault (other.targetTree, other.targetProperty, other.undoManager, + other.defaultValue, other.delimiter); + + return *this; + } + private: //============================================================================== ValueTree targetTree; @@ -191,7 +200,7 @@ private: String delimiter; //============================================================================== - void referToWithDefault (ValueTree& v, const Identifier& i, UndoManager* um, + void referToWithDefault (const ValueTree& v, const Identifier& i, UndoManager* um, const var& defaultVal, StringRef del) { targetTree = v; @@ -225,6 +234,9 @@ private: return arr; } + + //============================================================================== + JUCE_DECLARE_WEAK_REFERENCEABLE (ValueWithDefault) }; } // namespace juce diff --git a/modules/juce_gui_basics/properties/juce_ChoicePropertyComponent.cpp b/modules/juce_gui_basics/properties/juce_ChoicePropertyComponent.cpp index 9e1d7277c9..0e4705a893 100644 --- a/modules/juce_gui_basics/properties/juce_ChoicePropertyComponent.cpp +++ b/modules/juce_gui_basics/properties/juce_ChoicePropertyComponent.cpp @@ -83,6 +83,9 @@ public: var getValue() const override { + if (valueWithDefault == nullptr) + return {}; + if (valueWithDefault->isUsingDefault()) return -1; @@ -97,6 +100,9 @@ public: void setValue (const var& newValue) override { + if (valueWithDefault == nullptr) + return; + auto newValueInt = static_cast (newValue); if (newValueInt == -1) @@ -115,7 +121,7 @@ public: private: void valueChanged (Value&) override { sendChangeMessage (true); } - ValueWithDefault* valueWithDefault = nullptr; + WeakReference valueWithDefault; Value sourceValue; Array mappings; diff --git a/modules/juce_gui_basics/properties/juce_ChoicePropertyComponent.h b/modules/juce_gui_basics/properties/juce_ChoicePropertyComponent.h index 8f0406aa72..3fa1f9a8a2 100644 --- a/modules/juce_gui_basics/properties/juce_ChoicePropertyComponent.h +++ b/modules/juce_gui_basics/properties/juce_ChoicePropertyComponent.h @@ -84,7 +84,6 @@ public: default value with an ID of -1. @param valueToControl the ValueWithDefault object that contains the Value object that the combo box will read and control. - NB: this object must outlive the ChoicePropertyComponent. @param propertyName the name of the property @param choices the list of possible values that the drop-down list will contain @param correspondingValues a list of values corresponding to each item in the 'choices' StringArray. @@ -154,7 +153,7 @@ private: ComboBox comboBox; bool isCustomClass = false; - ValueWithDefault* valueWithDefault = nullptr; + WeakReference valueWithDefault; //============================================================================== JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChoicePropertyComponent) diff --git a/modules/juce_gui_basics/properties/juce_MultiChoicePropertyComponent.cpp b/modules/juce_gui_basics/properties/juce_MultiChoicePropertyComponent.cpp index 0bc7bc1bbc..7806ea09b6 100644 --- a/modules/juce_gui_basics/properties/juce_MultiChoicePropertyComponent.cpp +++ b/modules/juce_gui_basics/properties/juce_MultiChoicePropertyComponent.cpp @@ -118,6 +118,9 @@ public: var getValue() const override { + if (valueWithDefault == nullptr) + return {}; + auto v = valueWithDefault->get(); if (auto* arr = v.getArray()) @@ -134,6 +137,9 @@ public: void setValue (const var& newValue) override { + if (valueWithDefault == nullptr) + return; + auto v = valueWithDefault->get(); OptionalScopedPointer> arrayToControl; @@ -191,7 +197,7 @@ private: } //============================================================================== - ValueWithDefault* valueWithDefault = nullptr; + WeakReference valueWithDefault; var varToControl; Value sourceValue; diff --git a/modules/juce_gui_basics/properties/juce_MultiChoicePropertyComponent.h b/modules/juce_gui_basics/properties/juce_MultiChoicePropertyComponent.h index 894290a68d..7896eeac8e 100644 --- a/modules/juce_gui_basics/properties/juce_MultiChoicePropertyComponent.h +++ b/modules/juce_gui_basics/properties/juce_MultiChoicePropertyComponent.h @@ -67,7 +67,6 @@ public: /** Creates the component using a ValueWithDefault object. This will select the default options. @param valueToControl the ValueWithDefault object that contains the Value object that the ToggleButtons will read and control. - NB: This object must outlive the MultiChoicePropertyComponent. @param propertyName the name of the property @param choices the list of possible values that will be represented @param correspondingValues a list of values corresponding to each item in the 'choices' StringArray. @@ -119,7 +118,7 @@ private: void lookAndFeelChanged() override; //============================================================================== - ValueWithDefault* valueWithDefault = nullptr; + WeakReference valueWithDefault; int maxHeight = 0; int numHidden = 0; diff --git a/modules/juce_gui_basics/properties/juce_TextPropertyComponent.cpp b/modules/juce_gui_basics/properties/juce_TextPropertyComponent.cpp index 8960a64355..b1bd654241 100644 --- a/modules/juce_gui_basics/properties/juce_TextPropertyComponent.cpp +++ b/modules/juce_gui_basics/properties/juce_TextPropertyComponent.cpp @@ -131,11 +131,17 @@ public: var getValue() const override { - return valueWithDefault->isUsingDefault() ? var() : valueWithDefault->get(); + if (valueWithDefault == nullptr || valueWithDefault->isUsingDefault()) + return {}; + + return valueWithDefault->get(); } void setValue (const var& newValue) override { + if (valueWithDefault == nullptr) + return; + if (newValue.toString().isEmpty()) valueWithDefault->resetToDefault(); else @@ -143,7 +149,7 @@ public: } private: - ValueWithDefault* valueWithDefault = nullptr; + WeakReference valueWithDefault; //============================================================================== JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RemapperValueSourceWithDefault) diff --git a/modules/juce_gui_basics/properties/juce_TextPropertyComponent.h b/modules/juce_gui_basics/properties/juce_TextPropertyComponent.h index fb67ef5dfc..cfcb569cb8 100644 --- a/modules/juce_gui_basics/properties/juce_TextPropertyComponent.h +++ b/modules/juce_gui_basics/properties/juce_TextPropertyComponent.h @@ -75,7 +75,6 @@ public: /** Creates a text property component with a default value. @param valueToControl The ValueWithDefault that is controlled by the TextPropertyComponent. - NB: this object must outlive the TextPropertyComponent. @param propertyName The name of the property @param maxNumChars If not zero, then this specifies the maximum allowable length of the string. If zero, then the string will have no length limit. @@ -183,7 +182,7 @@ private: std::unique_ptr textEditor; ListenerList listenerList; - ValueWithDefault* valueWithDefault = nullptr; + WeakReference valueWithDefault; //============================================================================== JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TextPropertyComponent)