diff --git a/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.cpp b/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.cpp index d3d5dcd3ca..5f225a8fb7 100644 --- a/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.cpp +++ b/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.cpp @@ -248,7 +248,7 @@ void AudioDeviceManager::insertDefaultDeviceNames (AudioDeviceSetup& setup) cons XmlElement* AudioDeviceManager::createStateXml() const { - return lastExplicitSettings != nullptr ? new XmlElement (*lastExplicitSettings) : nullptr; + return lastExplicitSettings.createCopy(); } //============================================================================== diff --git a/modules/juce_core/memory/juce_Memory.h b/modules/juce_core/memory/juce_Memory.h index 7c8d102545..830c434570 100644 --- a/modules/juce_core/memory/juce_Memory.h +++ b/modules/juce_core/memory/juce_Memory.h @@ -75,6 +75,11 @@ inline Type* addBytesToPointer (Type* pointer, int bytes) noexcept { return (Ty template inline int getAddressDifference (Type1* pointer1, Type2* pointer2) noexcept { return (int) (((const char*) pointer1) - (const char*) pointer2); } +/** If a pointer is non-null, this returns a new copy of the object that it points to, or safely returns + nullptr if the pointer is null. +*/ +template +inline Type* createCopyIfNotNull (Type* pointer) { return pointer != nullptr ? new Type (*pointer) : nullptr; } //============================================================================== /* In a Windows DLL build, we'll expose some malloc/free functions that live inside the DLL, and use these for diff --git a/modules/juce_core/memory/juce_ScopedPointer.h b/modules/juce_core/memory/juce_ScopedPointer.h index 25c99f7085..38c06768f4 100644 --- a/modules/juce_core/memory/juce_ScopedPointer.h +++ b/modules/juce_core/memory/juce_ScopedPointer.h @@ -169,6 +169,11 @@ public: std::swap (object, other.object); } + /** If the pointer is non-null, this will attempt to return a new copy of the object that is pointed to. + If the pointer is null, this will safely return a nullptr. + */ + inline ObjectType* createCopy() const { return createCopyIfNotNull (object); } + private: //============================================================================== ObjectType* object; diff --git a/modules/juce_data_structures/values/juce_ValueTree.cpp b/modules/juce_data_structures/values/juce_ValueTree.cpp index 78543355f6..74f2cde178 100644 --- a/modules/juce_data_structures/values/juce_ValueTree.cpp +++ b/modules/juce_data_structures/values/juce_ValueTree.cpp @@ -708,7 +708,7 @@ bool ValueTree::isEquivalentTo (const ValueTree& other) const ValueTree ValueTree::createCopy() const { - return ValueTree (object != nullptr ? new SharedObject (*object) : nullptr); + return ValueTree (createCopyIfNotNull (object.getObject())); } bool ValueTree::hasType (const Identifier& typeName) const diff --git a/modules/juce_graphics/colour/juce_FillType.cpp b/modules/juce_graphics/colour/juce_FillType.cpp index 66ce54ce58..64b26de80e 100644 --- a/modules/juce_graphics/colour/juce_FillType.cpp +++ b/modules/juce_graphics/colour/juce_FillType.cpp @@ -48,7 +48,7 @@ FillType::FillType (const Image& image_, const AffineTransform& transform_) noex FillType::FillType (const FillType& other) : colour (other.colour), - gradient (other.gradient != nullptr ? new ColourGradient (*other.gradient) : nullptr), + gradient (other.gradient.createCopy()), image (other.image), transform (other.transform) { @@ -59,7 +59,7 @@ FillType& FillType::operator= (const FillType& other) if (this != &other) { colour = other.colour; - gradient = (other.gradient != nullptr ? new ColourGradient (*other.gradient) : nullptr); + gradient = other.gradient.createCopy(); image = other.image; transform = other.transform; } diff --git a/modules/juce_opengl/opengl/juce_OpenGLGraphicsContext.cpp b/modules/juce_opengl/opengl/juce_OpenGLGraphicsContext.cpp index 510037b338..08dd6777c5 100644 --- a/modules/juce_opengl/opengl/juce_OpenGLGraphicsContext.cpp +++ b/modules/juce_opengl/opengl/juce_OpenGLGraphicsContext.cpp @@ -1873,8 +1873,7 @@ public: : clip (other.clip), transform (other.transform), font (other.font), fillType (other.fillType), interpolationQuality (other.interpolationQuality), state (other.state), transparencyLayerAlpha (other.transparencyLayerAlpha), - transparencyLayer (other.transparencyLayer), - previousTarget (other.previousTarget != nullptr ? new OpenGLTarget (*other.previousTarget) : nullptr) + transparencyLayer (other.transparencyLayer), previousTarget (other.previousTarget.createCopy()) { }