From 924047524ac4d10f9de55882de0f6f1df48941dd Mon Sep 17 00:00:00 2001 From: Ilari Liusvaara Date: Sat, 13 Oct 2012 09:36:37 +0300 Subject: [PATCH] globalwrap: Warn about recursive initialization --- include/library/globalwrap.hpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/include/library/globalwrap.hpp b/include/library/globalwrap.hpp index 09b125f9..ac712d52 100644 --- a/include/library/globalwrap.hpp +++ b/include/library/globalwrap.hpp @@ -1,6 +1,8 @@ #ifndef _library__globalwrap__hpp__included__ #define _library__globalwrap__hpp__included__ +#include + /** * Wrapper for glboal/module-local objects accessable in global ctor context. */ @@ -13,8 +15,7 @@ public: */ globalwrap() throw(std::bad_alloc) { - if(!storage) - storage = new T(); + (*this)(); } /** * Get the wrapped object. @@ -24,12 +25,19 @@ public: */ T& operator()() throw(std::bad_alloc) { - if(!storage) + if(!storage) { + if(!state) //State initializes to 0. + state = 1; + else if(state == 1) + std::cerr << "Warning: Recursive use of globalwrap." << std::endl; storage = new T(); + state = 2; + } return *storage; } private: T* storage; + unsigned state; }; #endif -- 2.11.4.GIT