diff --git a/extras/Projucer/Source/Utility/jucer_JucerTreeViewBase.cpp b/extras/Projucer/Source/Utility/jucer_JucerTreeViewBase.cpp index 0ffc16123a..bf638e5cb7 100644 --- a/extras/Projucer/Source/Utility/jucer_JucerTreeViewBase.cpp +++ b/extras/Projucer/Source/Utility/jucer_JucerTreeViewBase.cpp @@ -72,7 +72,6 @@ JucerTreeViewBase::JucerTreeViewBase() : textX (0) JucerTreeViewBase::~JucerTreeViewBase() { - masterReference.clear(); } void JucerTreeViewBase::refreshSubItems() diff --git a/extras/Projucer/Source/Utility/jucer_JucerTreeViewBase.h b/extras/Projucer/Source/Utility/jucer_JucerTreeViewBase.h index 754a23360b..ae05b882cd 100644 --- a/extras/Projucer/Source/Utility/jucer_JucerTreeViewBase.h +++ b/extras/Projucer/Source/Utility/jucer_JucerTreeViewBase.h @@ -110,10 +110,9 @@ private: friend class ItemSelectionTimer; ScopedPointer delayedSelectionTimer; - WeakReference::Master masterReference; - friend class WeakReference; - void invokeShowDocument(); + + JUCE_DECLARE_WEAK_REFERENCEABLE (JucerTreeViewBase) }; //============================================================================== diff --git a/modules/juce_audio_devices/native/juce_win32_ASIO.cpp b/modules/juce_audio_devices/native/juce_win32_ASIO.cpp index 11645bb6c2..a9c7ccd8d0 100644 --- a/modules/juce_audio_devices/native/juce_win32_ASIO.cpp +++ b/modules/juce_audio_devices/native/juce_win32_ASIO.cpp @@ -1440,16 +1440,7 @@ struct ASIOAudioIODevice::ASIOCallbackFunctions ::Master masterReference; + JUCE_DECLARE_WEAK_REFERENCEABLE (ASIOAudioIODeviceType) private: StringArray deviceNames; Array classIds; - bool hasScanned; + bool hasScanned = false; //============================================================================== static bool checkClassIsOk (const String& classId) diff --git a/modules/juce_core/memory/juce_WeakReference.h b/modules/juce_core/memory/juce_WeakReference.h index c743fc6c9b..791b541f97 100644 --- a/modules/juce_core/memory/juce_WeakReference.h +++ b/modules/juce_core/memory/juce_WeakReference.h @@ -59,6 +59,8 @@ namespace juce friend class WeakReference; }; + OR: just use the handy JUCE_DECLARE_WEAK_REFERENCEABLE macro to do all this for you. + // Here's an example of using a pointer.. MyObject* n = new MyObject(); @@ -202,4 +204,31 @@ private: } }; + +//============================================================================== +/** + Macro to easily allow a class to be made weak-referenceable. + This can be inserted in a class definition to add the requisite weak-ref boilerplate to that class. + e.g. + + @code + class MyObject + { + public: + MyObject(); + ~MyObject(); + + private: + JUCE_DECLARE_WEAK_REFERENCEABLE (MyObject) + }; + @endcode + + @see WeakReference, WeakReference::Master +*/ +#define JUCE_DECLARE_WEAK_REFERENCEABLE(Class) \ + struct WeakRefMaster : public WeakReference::Master { ~WeakRefMaster() { this->clear(); } }; \ + WeakRefMaster masterReference; \ + friend class WeakReference; \ + + } // namespace juce diff --git a/modules/juce_events/broadcasters/juce_ActionBroadcaster.cpp b/modules/juce_events/broadcasters/juce_ActionBroadcaster.cpp index 2b4613e82c..9efb3a9e1d 100644 --- a/modules/juce_events/broadcasters/juce_ActionBroadcaster.cpp +++ b/modules/juce_events/broadcasters/juce_ActionBroadcaster.cpp @@ -59,8 +59,6 @@ ActionBroadcaster::~ActionBroadcaster() { // all event-based objects must be deleted BEFORE juce is shut down! jassert (MessageManager::getInstanceWithoutCreating() != nullptr); - - masterReference.clear(); } void ActionBroadcaster::addActionListener (ActionListener* const listener) diff --git a/modules/juce_events/broadcasters/juce_ActionBroadcaster.h b/modules/juce_events/broadcasters/juce_ActionBroadcaster.h index 3267d86139..3936253a2f 100644 --- a/modules/juce_events/broadcasters/juce_ActionBroadcaster.h +++ b/modules/juce_events/broadcasters/juce_ActionBroadcaster.h @@ -64,15 +64,13 @@ public: private: //============================================================================== - friend class WeakReference; - WeakReference::Master masterReference; - class ActionMessage; friend class ActionMessage; SortedSet actionListeners; CriticalSection actionListenerLock; + JUCE_DECLARE_WEAK_REFERENCEABLE (ActionBroadcaster) JUCE_DECLARE_NON_COPYABLE (ActionBroadcaster) }; diff --git a/modules/juce_events/interprocess/juce_InterprocessConnection.cpp b/modules/juce_events/interprocess/juce_InterprocessConnection.cpp index 6f98aed632..8764d061c1 100644 --- a/modules/juce_events/interprocess/juce_InterprocessConnection.cpp +++ b/modules/juce_events/interprocess/juce_InterprocessConnection.cpp @@ -26,22 +26,16 @@ namespace juce struct InterprocessConnection::ConnectionThread : public Thread { ConnectionThread (InterprocessConnection& c) : Thread ("JUCE IPC"), owner (c) {} - void run() override { owner.runThread(); } -private: InterprocessConnection& owner; - JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ConnectionThread) }; //============================================================================== -InterprocessConnection::InterprocessConnection (const bool callbacksOnMessageThread, - const uint32 magicMessageHeaderNumber) - : callbackConnectionState (false), - useMessageThread (callbacksOnMessageThread), - magicMessageHeader (magicMessageHeaderNumber), - pipeReceiveMessageTimeout (-1) +InterprocessConnection::InterprocessConnection (bool callbacksOnMessageThread, uint32 magicMessageHeaderNumber) + : useMessageThread (callbacksOnMessageThread), + magicMessageHeader (magicMessageHeaderNumber) { thread = new ConnectionThread (*this); } @@ -207,7 +201,7 @@ struct ConnectionStateMessage : public MessageManager::MessageBase void messageCallback() override { - if (InterprocessConnection* const ipc = owner) + if (auto* ipc = owner.get()) { if (connectionMade) ipc->connectionMade(); @@ -256,7 +250,7 @@ struct DataDeliveryMessage : public Message void messageCallback() override { - if (InterprocessConnection* const ipc = owner) + if (auto* ipc = owner.get()) ipc->messageReceived (data); } @@ -331,7 +325,7 @@ void InterprocessConnection::runThread() { if (socket != nullptr) { - const int ready = socket->waitUntilReady (true, 0); + auto ready = socket->waitUntilReady (true, 0); if (ready < 0) { diff --git a/modules/juce_events/interprocess/juce_InterprocessConnection.h b/modules/juce_events/interprocess/juce_InterprocessConnection.h index 83cd910f31..7a5a1b72d3 100644 --- a/modules/juce_events/interprocess/juce_InterprocessConnection.h +++ b/modules/juce_events/interprocess/juce_InterprocessConnection.h @@ -176,15 +176,13 @@ public: private: //============================================================================== - WeakReference::Master masterReference; - friend class WeakReference; CriticalSection pipeAndSocketLock; ScopedPointer socket; ScopedPointer pipe; - bool callbackConnectionState; + bool callbackConnectionState = false; const bool useMessageThread; const uint32 magicMessageHeader; - int pipeReceiveMessageTimeout; + int pipeReceiveMessageTimeout = -1; friend class InterprocessConnectionServer; void initialiseWithSocket (StreamingSocket*); @@ -202,6 +200,7 @@ private: void runThread(); int writeData (void*, int); + JUCE_DECLARE_WEAK_REFERENCEABLE (InterprocessConnection) JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InterprocessConnection) }; diff --git a/modules/juce_gui_basics/commands/juce_ApplicationCommandTarget.cpp b/modules/juce_gui_basics/commands/juce_ApplicationCommandTarget.cpp index 17cd9a0a56..c074dfc7af 100644 --- a/modules/juce_gui_basics/commands/juce_ApplicationCommandTarget.cpp +++ b/modules/juce_gui_basics/commands/juce_ApplicationCommandTarget.cpp @@ -49,14 +49,8 @@ private: }; //============================================================================== -ApplicationCommandTarget::ApplicationCommandTarget() -{ -} - -ApplicationCommandTarget::~ApplicationCommandTarget() -{ - masterReference.clear(); -} +ApplicationCommandTarget::ApplicationCommandTarget() {} +ApplicationCommandTarget::~ApplicationCommandTarget() {} //============================================================================== bool ApplicationCommandTarget::tryToInvoke (const InvocationInfo& info, const bool async) diff --git a/modules/juce_gui_basics/commands/juce_ApplicationCommandTarget.h b/modules/juce_gui_basics/commands/juce_ApplicationCommandTarget.h index bec7308406..1b31c1f64e 100644 --- a/modules/juce_gui_basics/commands/juce_ApplicationCommandTarget.h +++ b/modules/juce_gui_basics/commands/juce_ApplicationCommandTarget.h @@ -231,14 +231,12 @@ public: private: //============================================================================== - WeakReference::Master masterReference; - friend class WeakReference; - class CommandMessage; friend class CommandMessage; bool tryToInvoke (const InvocationInfo&, bool async); + JUCE_DECLARE_WEAK_REFERENCEABLE (ApplicationCommandTarget) JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ApplicationCommandTarget) }; diff --git a/modules/juce_gui_basics/layout/juce_ComponentAnimator.cpp b/modules/juce_gui_basics/layout/juce_ComponentAnimator.cpp index 936660d221..3d7961f5bd 100644 --- a/modules/juce_gui_basics/layout/juce_ComponentAnimator.cpp +++ b/modules/juce_gui_basics/layout/juce_ComponentAnimator.cpp @@ -32,11 +32,6 @@ class ComponentAnimator::AnimationTask public: AnimationTask (Component* c) noexcept : component (c) {} - ~AnimationTask() - { - masterReference.clear(); - } - void reset (const Rectangle& finalBounds, float finalAlpha, int millisecondsToSpendMoving, @@ -186,9 +181,6 @@ public: JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ProxyComponent) }; - WeakReference::Master masterReference; - friend class WeakReference; - WeakReference component; ScopedPointer proxy; @@ -208,6 +200,7 @@ private: + (time - 0.5) * (midSpeed + (time - 0.5) * (endSpeed - midSpeed)); } + JUCE_DECLARE_WEAK_REFERENCEABLE (AnimationTask) JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AnimationTask) }; diff --git a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.cpp b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.cpp index d2faffbaab..11e9744434 100644 --- a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.cpp +++ b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.cpp @@ -37,7 +37,6 @@ extern GetTypefaceForFont juce_getTypefaceForFont; //============================================================================== LookAndFeel::LookAndFeel() - : useNativeAlertWindows (false) { /* if this fails it means you're trying to create a LookAndFeel object before the static Colours have been initialised. That ain't gonna work. It probably @@ -51,7 +50,6 @@ LookAndFeel::LookAndFeel() LookAndFeel::~LookAndFeel() { - masterReference.clear(); } //============================================================================== diff --git a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.h b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.h index 997180a517..020c3991cf 100644 --- a/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.h +++ b/modules/juce_gui_basics/lookandfeel/juce_LookAndFeel.h @@ -212,9 +212,6 @@ public: private: //============================================================================== - friend class WeakReference; - WeakReference::Master masterReference; - struct ColourSetting { int colourID; @@ -226,8 +223,9 @@ private: SortedSet colours; String defaultSans, defaultSerif, defaultFixed; - bool useNativeAlertWindows; + bool useNativeAlertWindows = false; + JUCE_DECLARE_WEAK_REFERENCEABLE (LookAndFeel) JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LookAndFeel) };