From 8ef29c6f93aff6c17a1cefcb43c487e32dfd0b0e Mon Sep 17 00:00:00 2001 From: swenzel Date: Thu, 19 Dec 2024 13:31:08 +0100 Subject: [PATCH] Make sure PrimaryGenerator is destructed Makes sure that PrimaryGenerators are destructed in o2-sim. Note that all FairGenerator pointers registered in the PrimaryGenerator are automatically destructed as well. --- Generators/src/PrimaryGenerator.cxx | 2 +- run/O2PrimaryServerDevice.h | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Generators/src/PrimaryGenerator.cxx b/Generators/src/PrimaryGenerator.cxx index 6fc12ed32980a..21974472e7def 100644 --- a/Generators/src/PrimaryGenerator.cxx +++ b/Generators/src/PrimaryGenerator.cxx @@ -40,7 +40,7 @@ namespace eventgen PrimaryGenerator::~PrimaryGenerator() { /** destructor **/ - + LOG(info) << "Destructing PrimaryGenerator"; if (mEmbedFile && mEmbedFile->IsOpen()) { mEmbedFile->Close(); delete mEmbedFile; diff --git a/run/O2PrimaryServerDevice.h b/run/O2PrimaryServerDevice.h index 53b86d1f23591..1db1109f573e8 100644 --- a/run/O2PrimaryServerDevice.h +++ b/run/O2PrimaryServerDevice.h @@ -105,7 +105,7 @@ class O2PrimaryServerDevice final : public fair::mq::Device if (conf.getGenerator().compare("extkin") != 0 || conf.getGenerator().compare("extkinO2") != 0) { auto iter = mPrimGeneratorCache.find(conf.getGenerator()); if (iter != mPrimGeneratorCache.end()) { - mPrimGen = iter->second; + mPrimGen = iter->second.get(); LOG(info) << "Found cached generator for " << conf.getGenerator(); } } @@ -133,7 +133,9 @@ class O2PrimaryServerDevice final : public fair::mq::Device mPrimGen->Init(); - mPrimGeneratorCache[conf.getGenerator()] = mPrimGen; + std::unique_ptr ptr_wrapper; + ptr_wrapper.reset(mPrimGen); + mPrimGeneratorCache[conf.getGenerator()] = std::move(ptr_wrapper); } mPrimGen->SetEvent(&mEventHeader); @@ -668,11 +670,11 @@ class O2PrimaryServerDevice final : public fair::mq::Device // Keeps various generators instantiated in memory // useful when running simulation as a service (when generators - // change between batches) + // change between batches). Also takes care of resource management of Primary generators via unique ptr // TODO: some care needs to be taken (or the user warned) that the caching is based on generator name // and that parameter-based reconfiguration is not yet implemented (for which we would need to hash all // configuration parameters as well) - std::map mPrimGeneratorCache; + std::map> mPrimGeneratorCache; std::atomic mState{O2PrimaryServerState::Initializing}; std::atomic mWaitingControlInput{0};