
I have singleton class X. The instance of X is constructed the first time it is used, and is destroyed during the static destruction phase at app termination. X::X() reads data from a ptree X::~X() writes data to a ptree My app crashes in X::~X(). The reason is that ptree uses static variables, for instance in the following function: ptree_implementation.h: template<class Ptree> inline const Ptree &empty_ptree() { static Ptree pt; return pt; } Would it be possible to remove the static variables from the ptree code? That would make it safe to use ptree during the static destruction phase. There are several static variables in the ptree code, and they all seem to be optimizations. Maybe they are not needed. --Johan

On 12/11/06, Johan Råde <rade@maths.lth.se> wrote:
I have singleton class X. The instance of X is constructed the first time it is used, and is destroyed during the static destruction phase at app termination.
X::X() reads data from a ptree X::~X() writes data to a ptree
My app crashes in X::~X().
The reason is that ptree uses static variables.
Static variables are destroyed in reverse order of the completion of their constructors. Therefore, if you make sure to call any ptree functions used by ~X() in X(), this will make sure that the static variables needed by ~X() are created before construction of X is complete. That gives you the guarantee that X will be destroyed before any of the static vars it depends on. -Jason

Johan Råde wrote:
ptree_implementation.h:
template<class Ptree> inline const Ptree &empty_ptree() { static Ptree pt; return pt; }
Would it be possible to remove the static variables from the ptree code? That would make it safe to use ptree during the static destruction phase.
Considering the fact that construction of static variables like this is not thread-safe, this even more important.
participants (3)
-
Jason Hise
-
Johan Råde
-
Yuval Ronen