diff --git a/Modules/Core/Common/include/itkObject.h b/Modules/Core/Common/include/itkObject.h index e18d5ff5f44..027c2050225 100644 --- a/Modules/Core/Common/include/itkObject.h +++ b/Modules/Core/Common/include/itkObject.h @@ -34,6 +34,7 @@ #include "itkSingletonMacro.h" #include +#include // For unique_ptr. namespace itk { @@ -273,7 +274,8 @@ class ITKCommon_EXPORT Object : public LightObject /** Implementation class for Subject/Observer Pattern. * This is only allocated if used. */ - SubjectImplementation * m_SubjectImplementation{ nullptr }; + std::unique_ptr m_SubjectImplementation{ nullptr }; + /** * Implementation for holding Object MetaData * @see itk::MetaDataDictionary @@ -281,7 +283,7 @@ class ITKCommon_EXPORT Object : public LightObject * @see itk::MetaDataObject * This is only allocated if used. */ - mutable MetaDataDictionary * m_MetaDataDictionary{ nullptr }; + mutable std::unique_ptr m_MetaDataDictionary{ nullptr }; std::string m_ObjectName; }; diff --git a/Modules/Core/Common/src/itkObject.cxx b/Modules/Core/Common/src/itkObject.cxx index ee4376812fc..ab92c8cc543 100644 --- a/Modules/Core/Common/src/itkObject.cxx +++ b/Modules/Core/Common/src/itkObject.cxx @@ -478,7 +478,7 @@ Object::AddObserver(const EventObject & event, Command * cmd) { if (!this->m_SubjectImplementation) { - this->m_SubjectImplementation = new SubjectImplementation; + this->m_SubjectImplementation = std::make_unique(); } return this->m_SubjectImplementation->AddObserver(event, cmd); } @@ -489,7 +489,7 @@ Object::AddObserver(const EventObject & event, Command * cmd) const if (!this->m_SubjectImplementation) { auto * me = const_cast(this); - me->m_SubjectImplementation = new SubjectImplementation; + me->m_SubjectImplementation = std::make_unique(); } return this->m_SubjectImplementation->AddObserver(event, cmd); } @@ -583,8 +583,6 @@ Object::Object() Object::~Object() { itkDebugMacro(<< "Destructing!"); - delete m_SubjectImplementation; - delete m_MetaDataDictionary; } /** @@ -611,7 +609,7 @@ Object::GetMetaDataDictionary() { if (m_MetaDataDictionary == nullptr) { - m_MetaDataDictionary = new MetaDataDictionary; + m_MetaDataDictionary = std::make_unique(); } return *m_MetaDataDictionary; } @@ -621,7 +619,7 @@ Object::GetMetaDataDictionary() const { if (m_MetaDataDictionary == nullptr) { - m_MetaDataDictionary = new MetaDataDictionary; + m_MetaDataDictionary = std::make_unique(); } return *m_MetaDataDictionary; } @@ -631,7 +629,7 @@ Object::SetMetaDataDictionary(const MetaDataDictionary & rhs) { if (m_MetaDataDictionary == nullptr) { - m_MetaDataDictionary = new MetaDataDictionary(rhs); + m_MetaDataDictionary = std::make_unique(rhs); return; } *m_MetaDataDictionary = rhs; @@ -642,7 +640,7 @@ Object::SetMetaDataDictionary(MetaDataDictionary && rrhs) { if (m_MetaDataDictionary == nullptr) { - m_MetaDataDictionary = new MetaDataDictionary(std::move(rrhs)); + m_MetaDataDictionary = std::make_unique(std::move(rrhs)); return; } *m_MetaDataDictionary = std::move(rrhs);