Skip to content

Commit

Permalink
Avoid the double-free by a convenience class that is statically initi…
Browse files Browse the repository at this point in the history
…alized that passes the generator
  • Loading branch information
ibell committed Apr 18, 2017
1 parent c4d14b9 commit 27778ab
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 41 deletions.
9 changes: 9 additions & 0 deletions include/AbstractState.h
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,15 @@ class AbstractStateGenerator{
* publically accessible)
*/
void register_backend(const backend_families &bf, shared_ptr<AbstractStateGenerator> gen);

template <backend_families bf, class T>
class GeneratorInitializer{
public:
GeneratorInitializer(){
register_backend(bf, shared_ptr<AbstractStateGenerator>(new T()));
};
};


} /* namespace CoolProp */
#endif /* ABSTRACTSTATE_H_ */
5 changes: 2 additions & 3 deletions src/AbstractState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
namespace CoolProp {

/// This tiny class holds pointers to generators for the backends and can be used to look up
/// generators at runtime. This class should be populated through the use of static initialized
/// classes that were passed to register_backend
/// generators at runtime. This class should be populated through the use of static initialized

class BackendLibrary{
private:
std::map<backend_families, shared_ptr<AbstractStateGenerator> > backends;
Expand All @@ -40,7 +40,6 @@ class BackendLibrary{
};
static BackendLibrary backend_library;

/// Register the backend (should probably be done with static initialization)
void register_backend(const backend_families &bf, shared_ptr<AbstractStateGenerator> gen){
backend_library.add_backend(bf, gen);
};
Expand Down
18 changes: 8 additions & 10 deletions src/Backends/Cubics/CubicBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@
#include "Configuration.h"
#include "Backends/Helmholtz/VLERoutines.h"

static class SRKGenerator : public CoolProp::AbstractStateGenerator{
class SRKGenerator : public CoolProp::AbstractStateGenerator{
public:
SRKGenerator(){
register_backend(CoolProp::SRK_BACKEND_FAMILY, shared_ptr<AbstractStateGenerator>(this));
}
CoolProp::AbstractState * get_AbstractState(const std::vector<std::string> &fluid_names){
return new CoolProp::SRKBackend(fluid_names, CoolProp::get_config_double(R_U_CODATA));
};
} srk_gen;
static class PRGenerator : public CoolProp::AbstractStateGenerator{
};
static CoolProp::GeneratorInitializer<CoolProp::SRK_BACKEND_FAMILY, SRKGenerator> srk_gen;

class PRGenerator : public CoolProp::AbstractStateGenerator{
public:
PRGenerator(){
register_backend(CoolProp::PR_BACKEND_FAMILY, shared_ptr<AbstractStateGenerator>(this));
}
CoolProp::AbstractState * get_AbstractState(const std::vector<std::string> &fluid_names){
return new CoolProp::PengRobinsonBackend(fluid_names, CoolProp::get_config_double(R_U_CODATA));
};
} pr_gen;
};
static CoolProp::GeneratorInitializer<CoolProp::PR_BACKEND_FAMILY, PRGenerator> pr_gen;


void CoolProp::AbstractCubicBackend::setup(bool generate_SatL_and_SatV){
N = cubic->get_Tc().size();
Expand Down
10 changes: 4 additions & 6 deletions src/Backends/Cubics/VTPRBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@

static UNIFACLibrary::UNIFACParameterLibrary lib;

static class VTPRGenerator : public CoolProp::AbstractStateGenerator{
class VTPRGenerator : public CoolProp::AbstractStateGenerator{
public:
VTPRGenerator(){
register_backend(CoolProp::VTPR_BACKEND_FAMILY, shared_ptr<AbstractStateGenerator>(this));
}
CoolProp::AbstractState * get_AbstractState(const std::vector<std::string> &fluid_names){
return new CoolProp::VTPRBackend(fluid_names, CoolProp::get_config_double(R_U_CODATA));
};
} vtpr_gen; // This static initialization will cause the generator to register

} ;
// This static initialization will cause the generator to register
static CoolProp::GeneratorInitializer<CoolProp::VTPR_BACKEND_FAMILY, VTPRGenerator> vtpr_gen;

void CoolProp::VTPRBackend::setup(const std::vector<std::string> &names, bool generate_SatL_and_SatV){

Expand Down
10 changes: 4 additions & 6 deletions src/Backends/Helmholtz/HelmholtzEOSMixtureBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,8 @@ static int deriv_counter = 0;

namespace CoolProp {

// This static initialization will cause the generator to register
static class HEOSGenerator : public AbstractStateGenerator{
class HEOSGenerator : public AbstractStateGenerator{
public:
HEOSGenerator(){
register_backend(HEOS_BACKEND_FAMILY, shared_ptr<AbstractStateGenerator>(this));
}
AbstractState * get_AbstractState(const std::vector<std::string> &fluid_names){
if (fluid_names.size() == 1){
return new HelmholtzEOSBackend(fluid_names[0]);
Expand All @@ -55,7 +51,9 @@ static class HEOSGenerator : public AbstractStateGenerator{
return new HelmholtzEOSMixtureBackend(fluid_names);
}
};
} heos_gen;
};
// This static initialization will cause the generator to register
static CoolProp::GeneratorInitializer<CoolProp::HEOS_BACKEND_FAMILY, HEOSGenerator> heos_gen;

HelmholtzEOSMixtureBackend::HelmholtzEOSMixtureBackend(){
imposed_phase_index = iphase_not_imposed;
Expand Down
9 changes: 4 additions & 5 deletions src/Backends/IF97/IF97Backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
#include "AbstractState.h"
#include "DataStructures.h"

static class IF97BackendGenerator : public CoolProp::AbstractStateGenerator{
class IF97BackendGenerator : public CoolProp::AbstractStateGenerator{
public:
IF97BackendGenerator(){
register_backend(CoolProp::IF97_BACKEND_FAMILY, shared_ptr<AbstractStateGenerator>(this));
}
CoolProp::AbstractState * get_AbstractState(const std::vector<std::string> &fluid_names){
return new CoolProp::IF97Backend();
};
} if97_gen; // This static initialization will cause the generator to register
} ;
// This static initialization will cause the generator to register
static CoolProp::GeneratorInitializer<CoolProp::IF97_BACKEND_FAMILY, IF97BackendGenerator> if97_gen;
9 changes: 4 additions & 5 deletions src/Backends/Incompressible/IncompressibleBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@

namespace CoolProp {

static class IncompressibleBackendGenerator : public AbstractStateGenerator{
class IncompressibleBackendGenerator : public AbstractStateGenerator{
public:
IncompressibleBackendGenerator(){
register_backend(INCOMP_BACKEND_FAMILY, shared_ptr<AbstractStateGenerator>(this));
}
AbstractState * get_AbstractState(const std::vector<std::string> &fluid_names){
if (fluid_names.size() != 1){throw ValueError(format("For INCOMP backend, name vector must be one element long"));}
return new IncompressibleBackend(fluid_names[0]);
};
} incomp_gen; // This static initialization will cause the generator to register
};
// This static initialization will cause the generator to register
static GeneratorInitializer<INCOMP_BACKEND_FAMILY, IncompressibleBackendGenerator> incomp_gen;

IncompressibleBackend::IncompressibleBackend() {
throw NotImplementedError("Empty constructor is not implemented for incompressible fluids");
Expand Down
10 changes: 4 additions & 6 deletions src/Backends/REFPROP/REFPROPMixtureBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,8 @@ std::string get_REFPROP_HMX_BNC_path()

namespace CoolProp {

// This static initialization will cause the generator to register
static class REFPROPGenerator : public AbstractStateGenerator{
class REFPROPGenerator : public AbstractStateGenerator{
public:
REFPROPGenerator(){
register_backend(REFPROP_BACKEND_FAMILY, shared_ptr<AbstractStateGenerator>(this));
}
AbstractState * get_AbstractState(const std::vector<std::string> &fluid_names){
bool REFPROP_is_supported = REFPROPMixtureBackend::REFPROP_supported ();
if (fluid_names.size() == 1){
Expand All @@ -146,7 +142,9 @@ static class REFPROPGenerator : public AbstractStateGenerator{
return new REFPROPMixtureBackend(fluid_names);
}
};
} refprop_gen;
};
// This static initialization will cause the generator to register
static GeneratorInitializer<REFPROP_BACKEND_FAMILY, REFPROPGenerator> refprop_gen;


void REFPROPMixtureBackend::construct(const std::vector<std::string>& fluid_names) {
Expand Down

0 comments on commit 27778ab

Please sign in to comment.