Skip to content

Commit

Permalink
PyHeliosSimulation now trackes copies for proper destruction.
Browse files Browse the repository at this point in the history
  • Loading branch information
albertoesmp committed May 29, 2024
1 parent f2bdaf5 commit 508f203
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
39 changes: 36 additions & 3 deletions src/pybinds/PyHeliosSimulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,35 @@ PyHeliosSimulation::PyHeliosSimulation(
xmlreader = std::make_shared<XmlSurveyLoader>(surveyPath, assetsPath);
}
PyHeliosSimulation::~PyHeliosSimulation() {
if(playback != nullptr){
playback->mSurvey->scanner->getDetector()->shutdown();
playback->mSurvey->scanner->platform->scene->shutdown();
if(survey != nullptr){
// Release shared resources
bool sharedDetector = false;
bool sharedScene = false;
std::shared_ptr<AbstractDetector> ad = survey->scanner->getDetector();
std::shared_ptr<Scene> scene = survey->scanner->platform->scene;
for(PyHeliosSimulation * copy : copies){
if(copy->survey->scanner->getDetector() == ad){
sharedDetector = true;
}
if(copy->survey->scanner->platform->scene == scene){
sharedScene = true;
}
}
if(!sharedDetector){
survey->scanner->getDetector()->shutdown();
}
if(!sharedScene){
survey->scanner->platform->scene->shutdown();
}
// Update copy tracking for non-destroyed copies
for(PyHeliosSimulation * copy : copies){
for(size_t i = 0 ; i < copy->copies.size() ; ++i) {
if(copy->copies[i] == this){
copy->copies.erase(copy->copies.begin()+i);
break;
}
}
}
}
if(thread != nullptr) delete thread;
}
Expand Down Expand Up @@ -349,6 +375,7 @@ void PyHeliosSimulation::buildPulseThreadPool(){
// *** SIMULATION COPY *** //
// ************************* //
PyHeliosSimulation * PyHeliosSimulation::copy(){
// Copy
PyHeliosSimulation *phs = new PyHeliosSimulation(); // The copy itself
phs->xmlreader = std::make_shared<XmlSurveyLoader>(surveyPath, assetsPath);
phs->surveyPath = this->surveyPath;
Expand All @@ -364,6 +391,12 @@ PyHeliosSimulation * PyHeliosSimulation::copy(){
phs->setCallbackFrequency(getCallbackFrequency());
phs->survey = std::make_shared<Survey>(*survey);
phs->survey->scanner->initializeSequentialGenerators();
// Track copies
phs->copies = copies;
phs->copies.push_back(this);
for(PyHeliosSimulation *phsi : copies) phsi->copies.push_back(phs);
copies.push_back(phs);
// Return
return phs;
}

Expand Down
1 change: 1 addition & 0 deletions src/pybinds/PyHeliosSimulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class PyHeliosSimulation{
int parallelizationStrategy = 1;
int chunkSize = 32;
int warehouseFactor = 1;
std::vector<PyHeliosSimulation *> copies;
public:
bool finalOutput = true;
bool legacyEnergyModel = false;
Expand Down

0 comments on commit 508f203

Please sign in to comment.