Problem 2083

Summary: G4UImanager instantiated before main starts
Product: Geant4 Reporter: Sergio Losilla <sergio.losilla>
Component: interfacesAssignee: asai
Status: RESOLVED FIXED    
Severity: minor CC: asai, Gabriele.Cosmo, laurent.garnier, sergio.losilla
Priority: P4    
Version: 10.4   
Hardware: All   
OS: All   
Attachments: Proposed fix

Description Sergio Losilla 2018-08-31 20:32:48 CEST
Created attachment 508 [details]
Proposed fix

While investigating a related issue (https://bugzilla-geant4.kek.jp/show_bug.cgi?id=2082) I bumped into an obscure problem that might cause undesired behaviour.

Static variables which require the G4UImanager in their constructors might become instantiated before main starts. The two that got instantiated for me were these:

G4PlotManager::fgPlotParameters
G4ParticleHPManager::instance

I was able to trigger this issue using the Catch2 framework (https://github.com/catchorg/Catch2) and the following source file:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <catch.hpp>

#include "G4MTRunManager.hh"

TEST_CASE("run 1")
{
    G4MTRunManager man;
}
TEST_CASE("run 2")
{
    G4MTRunManager man;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

All the necessary files can be found here:
https://gitlab.com/loximann/test-runmanager

See the attached file for a patch with the proposed fix. The fix consists of declaring the static instances inside functions, so they are only instantiated when the functions are called for the first time. I avoided renaming and reformatting for clarity, so probably additional changes would be needed to conform to the Geant4 conventions.
Comment 1 Gabriele Cosmo 2018-10-12 08:19:51 CEST
The code referred in the patch was reviewed in the development version and will be available in the next release. Thanks for notifying this.

Please, note however that as already mentioned in ticket #2082 (https://bugzilla-geant4.kek.jp/show_bug.cgi?id=2082), Geant4 kernel classes are not meant for resetting/re-instantiation in the same application.
Comment 2 Sergio Losilla 2018-10-15 14:43:04 CEST
Thank you for reviewing the fix :-)