From c2ba24c0c2e8c6d98dfbdc7bc33a1b852301a7ff Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Thu, 10 Sep 2015 17:43:06 -0400 Subject: [PATCH] Fix possible memory leak in newPhase If importPhase threw an exception, then the ThermoPhase object allocated here would not be deleted. Switching to a smart pointer fixes this problem. --- src/thermo/ThermoFactory.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/thermo/ThermoFactory.cpp b/src/thermo/ThermoFactory.cpp index e55738634..8bfc2c527 100644 --- a/src/thermo/ThermoFactory.cpp +++ b/src/thermo/ThermoFactory.cpp @@ -168,19 +168,19 @@ std::string eosTypeString(int ieos, int length) ThermoPhase* newPhase(XML_Node& xmlphase) { string model = xmlphase.child("thermo")["model"]; - ThermoPhase* t = newThermoPhase(model); + unique_ptr t(newThermoPhase(model)); if (model == "singing cows") { throw CanteraError("ThermoPhase::newPhase", "Cows don't sing"); } else if (model == "HMW") { - HMWSoln* p = dynamic_cast(t); + HMWSoln* p = dynamic_cast(t.get()); p->constructPhaseXML(xmlphase,""); } else if (model == "IonsFromNeutralMolecule") { - IonsFromNeutralVPSSTP* p = dynamic_cast(t); + IonsFromNeutralVPSSTP* p = dynamic_cast(t.get()); p->constructPhaseXML(xmlphase,""); } else { - importPhase(xmlphase, t); + importPhase(xmlphase, t.get()); } - return t; + return t.release(); } ThermoPhase* newPhase(const std::string& infile, std::string id)