1717#include " gaction.h"
1818#include " gstreamer.h"
1919
20+ // c++
21+ #include < cstdio>
22+ #include < cstdlib>
23+
2024int main (int argc, char * argv[]) {
2125
2226 auto gopts = std::make_shared<GOptions>(argc, argv, gemc::defineOptions ());
@@ -37,14 +41,13 @@ int main(int argc, char* argv[]) {
3741 // random engine set by options
3842 gemc::start_random_engine (gopts, log);
3943
40- // Pre-load streamer plugins before Geant4 creates worker threads. Sanitized Linux
41- // builds can fail late dlopen() calls from workers with static TLS exhaustion.
42- auto preloaded_gstreamer_map = gstreamer::preloadGStreamerPlugins (gopts);
43- (void ) preloaded_gstreamer_map;
44-
4544 // init geant4 run manager with then number of threads coming from options. always fails if unavailable
4645 auto runManager = std::unique_ptr<G4RunManager>(G4RunManagerFactory::CreateRunManager (G4RunManagerType::Default, true , nthreads));
4746
47+ // Pre-load streamer plugins before Geant4 creates worker threads. Sanitized Linux
48+ // builds can fail late dlopen() calls from workers with static TLS exhaustion.
49+ // Declaring this after runManager makes it destruct before runManager at shutdown.
50+ auto preloaded_gstreamer_map = gstreamer::preloadGStreamerPlugins (gopts);
4851
4952 // must be a raw pointer because geant4 takes ownership
5053 auto gdetector = new GDetectorConstruction (gopts);
@@ -111,6 +114,7 @@ int main(int argc, char* argv[]) {
111114 spash_screen->finish (&gemcGui);
112115 app_result = QApplication::exec ();
113116
117+ preloaded_gstreamer_map.reset ();
114118 runManager.reset ();
115119 delete g4SceneProperties;
116120 delete uiQtSession;
@@ -133,10 +137,20 @@ int main(int argc, char* argv[]) {
133137 delete session;
134138 }
135139
136-
137- delete visManager;
140+ geventDispenser.reset ();
141+ preloaded_gstreamer_map.reset ();
142+ runManager.reset ();
143+ // Avoid explicit G4VisExecutive teardown in batch shutdown. On Linux/Geant4 11.4
144+ // this can double-free the process-wide G4Colour table during static exit.
138145
139146 log->info (0 , " Simulation completed, arrivederci! " );
140147
148+ #if defined(__linux__)
149+ if (!gui) {
150+ std::fflush (nullptr );
151+ std::_Exit (app_result);
152+ }
153+ #endif
154+
141155 return app_result;
142156}
0 commit comments