[ROOT] ROOT initialization and static objects

From: George A. Heintzelman (gah@bnl.gov)
Date: Wed Nov 01 2000 - 20:57:54 MET


Hi rooters,

I'm writing some TFunctions that I would like to have global static 
linkage. I.e., they are declared:

extern TF1 Myfunc;

in a .h file and defined with a complete definition in a .cxx file.

If I then load the library containing this static, I get the function 
in my global scope, great. But I find that this sometimes causes me to 
segvio when I leave ROOT.

I have determined what the problem is. C++ says that the order of 
invocation of constructors and destructors of global static objects is 
completely unspecified. It turns out that in this particular case at 
least, the destructor of gROOT is being called before my global 
object's destructor. Then, the latter's base class TFormula's 
destructor makes a call to the destroyed gROOT, causing the segvio.

Since this kind of idiom is a natural one for extending the list of 
functions available to ROOT, and workarounds are really fairly painful, 
I think the easiest suggestion is to alter the initialization sequence 
of gROOT so that these sorts of things don't happen. The best way to 
deal with it, I think, would be to take a nod from the initialization 
of the iostream system in the standard library. Basically, if you 
include <iostream>, you're guaranteed that iostream functions are valid 
for all constructors and destructors of static global objects defined 
in that file (after the #include of <iostream>, but it's hard to use 
them otherwise).  Basically this is done by a reference-count-using 
object for which there is one instance in each source code file 
including iostream. (See Stroustrup, TC++PL, section 21.5.2 for 
details.) I would propose doing this in RConfig.h or perhaps Rtypes.h; 
then users are 100% guaranteed that ROOT is initialized for all their 
source files.

This would require changes only to that header and rmain.cxx (which no 
longer requires a TROOT object to be declared).

Alternatively, if there are large numbers of objects which might be 
able to be used without gROOT defined, one could define a separate 
header which does this initialization, only included by headres for 
classes which need an initialized and non-destructed ROOT for their 
constructors/destructors to be valid.

George Heintzelman
gah@bnl.gov



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:36 MET