From f776670cd0cda31fccd9608d0c3cf072ec38c6f0 Mon Sep 17 00:00:00 2001
From: Markus Frank <Markus.Frank@cern.ch>
Date: Tue, 2 Nov 2021 09:14:17 +0100
Subject: [PATCH] Move termination handler from DDCore to DDG4 (Issue  #874)

---
 DDCore/src/DetectorImp.cpp | 23 -----------------------
 DDG4/src/Geant4Kernel.cpp  | 17 +++++++++++++++++
 2 files changed, 17 insertions(+), 23 deletions(-)

diff --git a/DDCore/src/DetectorImp.cpp b/DDCore/src/DetectorImp.cpp
index 6d0edb122..28d350e30 100644
--- a/DDCore/src/DetectorImp.cpp
+++ b/DDCore/src/DetectorImp.cpp
@@ -135,24 +135,6 @@ namespace {
     static Instances s_inst;
     return s_inst;
   }
-
-  void description_unexpected()    {
-    try  {
-      throw;
-    }  catch( exception& e )  {
-      cout << "\n"
-           << "**************************************************** \n"
-           << "*  A runtime error has occured :                     \n"
-           << "*    " << e.what()   << endl
-           << "*  the program will have to be terminated - sorry.   \n"
-           << "**************************************************** \n"
-           << endl ;
-
-      set_terminate(std::terminate);
-      // this provokes ROOT seg fault and stack trace (comment out to avoid it)
-      exit(1) ;
-    }
-  }
 }
 
 string dd4hep::versionString(){
@@ -226,11 +208,6 @@ DetectorImp::DetectorImp(const string& name)
   }
 #endif
 
-  if ( first )    {
-    first = false;
-    set_terminate( description_unexpected );
-  }
-  
   SetName(name.c_str());
   SetTitle("DD4hep detector description object");
   //DetectorGuard(this).lock(gGeoManager);
diff --git a/DDG4/src/Geant4Kernel.cpp b/DDG4/src/Geant4Kernel.cpp
index a94f8db22..b85a3fbfb 100644
--- a/DDG4/src/Geant4Kernel.cpp
+++ b/DDG4/src/Geant4Kernel.cpp
@@ -41,6 +41,21 @@ using namespace dd4hep::sim;
 namespace {
   G4Mutex kernel_mutex=G4MUTEX_INITIALIZER;
   dd4hep::dd4hep_ptr<Geant4Kernel> s_main_instance(0);
+  void description_unexpected()    {
+    try  {
+      throw;
+    }  catch( exception& e )  {
+      cout << "\n"
+           << "**************************************************** \n"
+           << "*  A runtime error has occured :                     \n"
+           << "*    " << e.what()   << endl
+           << "*  the program will have to be terminated - sorry.   \n"
+           << "**************************************************** \n"
+           << endl ;
+      // this provokes ROOT seg fault and stack trace (comment out to avoid it)
+      exit(1) ;
+    }
+  }
 }
 
 /// Standard constructor
@@ -147,6 +162,8 @@ Geant4Kernel& Geant4Kernel::instance(Detector& description) {
   if ( 0 == s_main_instance.get() )   {
     G4AutoLock protection_lock(&kernel_mutex);    {
       if ( 0 == s_main_instance.get() )   { // Need to check again!
+        /// Install here the termination handler
+        std::set_terminate(description_unexpected);
         s_main_instance.adopt(new Geant4Kernel(description));
       }
     }
-- 
GitLab