From 67ac7257313b80a235d10a6074da72291420f2f9 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Wed, 9 Nov 2016 18:42:06 -0500 Subject: [PATCH] [Thermo] Eliminate manual memory management in VPStandardStateTP --- include/cantera/thermo/VPStandardStateTP.h | 6 +-- src/thermo/VPStandardStateTP.cpp | 44 +++++++--------------- 2 files changed, 16 insertions(+), 34 deletions(-) diff --git a/include/cantera/thermo/VPStandardStateTP.h b/include/cantera/thermo/VPStandardStateTP.h index 45cace3dd..bc564a8ec 100644 --- a/include/cantera/thermo/VPStandardStateTP.h +++ b/include/cantera/thermo/VPStandardStateTP.h @@ -14,6 +14,7 @@ #include "ThermoPhase.h" #include "VPSSMgr.h" +#include "PDSS.h" namespace Cantera { @@ -54,7 +55,6 @@ public: VPStandardStateTP(const VPStandardStateTP& b); VPStandardStateTP& operator=(const VPStandardStateTP& b); - virtual ~VPStandardStateTP(); virtual ThermoPhase* duplMyselfAsThermoPhase() const; //@} @@ -300,14 +300,14 @@ protected: // -> suggest making this private! //! Pointer to the VPSS manager that calculates all of the standard state //! info efficiently. - mutable VPSSMgr* m_VPSS_ptr; + mutable std::unique_ptr m_VPSS_ptr; //! Storage for the PDSS objects for the species /*! * Storage is in species index order. VPStandardStateTp owns each of the * objects. Copy operations are deep. */ - std::vector m_PDSS_storage; + std::vector> m_PDSS_storage; }; } diff --git a/src/thermo/VPStandardStateTP.cpp b/src/thermo/VPStandardStateTP.cpp index 9de697a5c..231a8e7bb 100644 --- a/src/thermo/VPStandardStateTP.cpp +++ b/src/thermo/VPStandardStateTP.cpp @@ -21,8 +21,7 @@ VPStandardStateTP::VPStandardStateTP() : m_Pcurrent(OneAtm), m_Tlast_ss(-1.0), m_Plast_ss(-1.0), - m_P0(OneAtm), - m_VPSS_ptr(0) + m_P0(OneAtm) { } @@ -30,8 +29,7 @@ VPStandardStateTP::VPStandardStateTP(const VPStandardStateTP& b) : m_Pcurrent(OneAtm), m_Tlast_ss(-1.0), m_Plast_ss(-1.0), - m_P0(OneAtm), - m_VPSS_ptr(0) + m_P0(OneAtm) { VPStandardStateTP::operator=(b); } @@ -49,20 +47,13 @@ VPStandardStateTP& VPStandardStateTP::operator=(const VPStandardStateTP& b) m_Plast_ss = b.m_Plast_ss; m_P0 = b.m_P0; - // Duplicate the pdss objects - if (m_PDSS_storage.size() > 0) { - for (int k = 0; k < (int) m_PDSS_storage.size(); k++) { - delete m_PDSS_storage[k]; - } - } m_PDSS_storage.resize(m_kk); for (size_t k = 0; k < m_kk; k++) { - m_PDSS_storage[k] = b.m_PDSS_storage[k]->duplMyselfAsPDSS(); + m_PDSS_storage[k].reset(b.m_PDSS_storage[k]->duplMyselfAsPDSS()); } // Duplicate the VPSS Manager object that conducts the calculations - delete m_VPSS_ptr; - m_VPSS_ptr = (b.m_VPSS_ptr)->duplMyselfAsVPSSMgr(); + m_VPSS_ptr.reset(b.m_VPSS_ptr->duplMyselfAsVPSSMgr()); // The VPSSMgr object contains shallow pointers. Whenever you have // shallow pointers, they have to be fixed up to point to the correct @@ -74,7 +65,7 @@ VPStandardStateTP& VPStandardStateTP::operator=(const VPStandardStateTP& b) // referring back to this ThermoPhase's properties. This function also // sets m_VPSS_ptr so it occurs after m_VPSS_ptr is set. for (size_t k = 0; k < m_kk; k++) { - m_PDSS_storage[k]->initAllPtrs(this, m_VPSS_ptr, m_spthermo); + m_PDSS_storage[k]->initAllPtrs(this, m_VPSS_ptr.get(), m_spthermo); } // Ok, the VPSSMgr object is ready for business. We need to resync the @@ -85,14 +76,6 @@ VPStandardStateTP& VPStandardStateTP::operator=(const VPStandardStateTP& b) return *this; } -VPStandardStateTP::~VPStandardStateTP() -{ - for (int k = 0; k < (int) m_PDSS_storage.size(); k++) { - delete m_PDSS_storage[k]; - } - delete m_VPSS_ptr; -} - ThermoPhase* VPStandardStateTP::duplMyselfAsThermoPhase() const { return new VPStandardStateTP(*this); @@ -217,7 +200,7 @@ void VPStandardStateTP::initThermo() ThermoPhase::initThermo(); m_VPSS_ptr->initThermo(); for (size_t k = 0; k < m_kk; k++) { - PDSS* kPDSS = m_PDSS_storage[k]; + PDSS* kPDSS = m_PDSS_storage[k].get(); if (kPDSS) { kPDSS->initThermo(); } @@ -226,7 +209,7 @@ void VPStandardStateTP::initThermo() void VPStandardStateTP::setVPSSMgr(VPSSMgr* vp_ptr) { - m_VPSS_ptr = vp_ptr; + m_VPSS_ptr.reset(vp_ptr); } bool VPStandardStateTP::addSpecies(shared_ptr spec) @@ -278,20 +261,19 @@ void VPStandardStateTP::createInstallPDSS(size_t k, const XML_Node& s, const XML_Node* phaseNode_ptr) { if (m_PDSS_storage.size() < k+1) { - m_PDSS_storage.resize(k+1,0); + m_PDSS_storage.resize(k+1); } - delete m_PDSS_storage[k]; - m_PDSS_storage[k] = m_VPSS_ptr->createInstallPDSS(k, s, phaseNode_ptr); + m_PDSS_storage[k].reset(m_VPSS_ptr->createInstallPDSS(k, s, phaseNode_ptr)); } PDSS* VPStandardStateTP::providePDSS(size_t k) { - return m_PDSS_storage[k]; + return m_PDSS_storage[k].get(); } const PDSS* VPStandardStateTP::providePDSS(size_t k) const { - return m_PDSS_storage[k]; + return m_PDSS_storage[k].get(); } void VPStandardStateTP::invalidateCache() @@ -303,7 +285,7 @@ void VPStandardStateTP::invalidateCache() void VPStandardStateTP::initThermoXML(XML_Node& phaseNode, const std::string& id) { for (size_t k = 0; k < m_kk; k++) { - PDSS* kPDSS = m_PDSS_storage[k]; + PDSS* kPDSS = m_PDSS_storage[k].get(); AssertTrace(kPDSS != 0); if (kPDSS) { kPDSS->initThermoXML(phaseNode, id); @@ -315,7 +297,7 @@ void VPStandardStateTP::initThermoXML(XML_Node& phaseNode, const std::string& id VPSSMgr* VPStandardStateTP::provideVPSSMgr() { - return m_VPSS_ptr; + return m_VPSS_ptr.get(); } void VPStandardStateTP::_updateStandardStateThermo() const