Skip to content

Commit 9424b41

Browse files
committed
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.
1 parent 948d271 commit 9424b41

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

Generators/src/PrimaryGenerator.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace eventgen
4040
PrimaryGenerator::~PrimaryGenerator()
4141
{
4242
/** destructor **/
43-
43+
LOG(info) << "Destructing PrimaryGenerator";
4444
if (mEmbedFile && mEmbedFile->IsOpen()) {
4545
mEmbedFile->Close();
4646
delete mEmbedFile;

run/O2PrimaryServerDevice.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class O2PrimaryServerDevice final : public fair::mq::Device
105105
if (conf.getGenerator().compare("extkin") != 0 || conf.getGenerator().compare("extkinO2") != 0) {
106106
auto iter = mPrimGeneratorCache.find(conf.getGenerator());
107107
if (iter != mPrimGeneratorCache.end()) {
108-
mPrimGen = iter->second;
108+
mPrimGen = iter->second.get();
109109
LOG(info) << "Found cached generator for " << conf.getGenerator();
110110
}
111111
}
@@ -133,7 +133,9 @@ class O2PrimaryServerDevice final : public fair::mq::Device
133133

134134
mPrimGen->Init();
135135

136-
mPrimGeneratorCache[conf.getGenerator()] = mPrimGen;
136+
std::unique_ptr<o2::eventgen::PrimaryGenerator> ptr_wrapper;
137+
ptr_wrapper.reset(mPrimGen);
138+
mPrimGeneratorCache[conf.getGenerator()] = std::move(ptr_wrapper);
137139
}
138140
mPrimGen->SetEvent(&mEventHeader);
139141

@@ -668,11 +670,11 @@ class O2PrimaryServerDevice final : public fair::mq::Device
668670

669671
// Keeps various generators instantiated in memory
670672
// useful when running simulation as a service (when generators
671-
// change between batches)
673+
// change between batches). Also takes care of resource management of Primary generators via unique ptr
672674
// TODO: some care needs to be taken (or the user warned) that the caching is based on generator name
673675
// and that parameter-based reconfiguration is not yet implemented (for which we would need to hash all
674676
// configuration parameters as well)
675-
std::map<std::string, o2::eventgen::PrimaryGenerator*> mPrimGeneratorCache;
677+
std::map<std::string, std::unique_ptr<o2::eventgen::PrimaryGenerator>> mPrimGeneratorCache;
676678

677679
std::atomic<O2PrimaryServerState> mState{O2PrimaryServerState::Initializing};
678680
std::atomic<int> mWaitingControlInput{0};

0 commit comments

Comments
 (0)