From 508f2033d823ac6fe0f87c1eab4a548554dd94f2 Mon Sep 17 00:00:00 2001 From: albertoesmp Date: Wed, 29 May 2024 14:35:18 +0200 Subject: [PATCH] PyHeliosSimulation now trackes copies for proper destruction. --- src/pybinds/PyHeliosSimulation.cpp | 39 +++++++++++++++++++++++++++--- src/pybinds/PyHeliosSimulation.h | 1 + 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/pybinds/PyHeliosSimulation.cpp b/src/pybinds/PyHeliosSimulation.cpp index 7d400512f..da16ef7b0 100644 --- a/src/pybinds/PyHeliosSimulation.cpp +++ b/src/pybinds/PyHeliosSimulation.cpp @@ -59,9 +59,35 @@ PyHeliosSimulation::PyHeliosSimulation( xmlreader = std::make_shared(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 ad = survey->scanner->getDetector(); + std::shared_ptr 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; } @@ -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(surveyPath, assetsPath); phs->surveyPath = this->surveyPath; @@ -364,6 +391,12 @@ PyHeliosSimulation * PyHeliosSimulation::copy(){ phs->setCallbackFrequency(getCallbackFrequency()); phs->survey = std::make_shared(*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; } diff --git a/src/pybinds/PyHeliosSimulation.h b/src/pybinds/PyHeliosSimulation.h index f6eeeb29d..ed3bc51b4 100644 --- a/src/pybinds/PyHeliosSimulation.h +++ b/src/pybinds/PyHeliosSimulation.h @@ -52,6 +52,7 @@ class PyHeliosSimulation{ int parallelizationStrategy = 1; int chunkSize = 32; int warehouseFactor = 1; + std::vector copies; public: bool finalOutput = true; bool legacyEnergyModel = false;