summaryrefslogtreecommitdiffstats
path: root/src/corelib/global
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2013-02-27 15:48:17 -0800
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-02-28 23:43:46 +0100
commit50d3a2917e37cd173f373a83542314002b2ab6c7 (patch)
tree8f686b6790cfe6385a07199c02f156f703729620 /src/corelib/global
parentebfd85a499a4382ace09d443b1f35cd6b1848af6 (diff)
Fix Q_GLOBAL_STATIC support for exceptions
The problem was that the HolderBase destructor was getting called after the contained type's constructor threw an exception, as is required by RAII semantics (the base was fully initialized, so it had to be destroyed). That was required because we want to return a non-null pointer from operator() during destruction and return null after destruction, to keep compatibility with Qt 4. The solution is to only set the guard to Destroyed only if it is already at value Initialized. This way, if the HolderBase destructor is run as part of the stack unwinding, it knows that the construction did not complete. Change-Id: I9849b43ed7112bf9e70861b48a56a924c286617e Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'src/corelib/global')
-rw-r--r--src/corelib/global/qglobalstatic.cpp8
-rw-r--r--src/corelib/global/qglobalstatic.h3
2 files changed, 7 insertions, 4 deletions
diff --git a/src/corelib/global/qglobalstatic.cpp b/src/corelib/global/qglobalstatic.cpp
index 7caa2e9848..8474d132b4 100644
--- a/src/corelib/global/qglobalstatic.cpp
+++ b/src/corelib/global/qglobalstatic.cpp
@@ -288,9 +288,11 @@
structure so holder's destructor can set the guard variable to the value -2
(destroyed) when the type has finished destruction. Since we need to set
the guard \b after the destruction has finished, this code needs to be in a
- base struct's destructor. A holder structure is used to avoid creating two
- statics, which the ABI might require duplicating the thread-safe control
- structures for.
+ base struct's destructor. And it only sets to -2 (destroyed) if it finds
+ the guard at -1 (initialized): this is done to ensure that the guard isn't
+ set to -2 in the event the type's constructor threw an exception. A holder
+ structure is used to avoid creating two statics, which the ABI might
+ require duplicating the thread-safe control structures for.
The other implementation is similar to Qt 4's Q_GLOBAL_STATIC, but unlike
that one, it uses a \l QBasicMutex to provide locking. It is also more
diff --git a/src/corelib/global/qglobalstatic.h b/src/corelib/global/qglobalstatic.h
index 336512eac3..a6268e057e 100644
--- a/src/corelib/global/qglobalstatic.h
+++ b/src/corelib/global/qglobalstatic.h
@@ -68,7 +68,8 @@ enum GuardValues {
{ \
struct HolderBase { \
~HolderBase() Q_DECL_NOTHROW \
- { guard.store(QtGlobalStatic::Destroyed); } \
+ { if (guard.load() == QtGlobalStatic::Initialized) \
+ guard.store(QtGlobalStatic::Destroyed); } \
}; \
static struct Holder : public HolderBase { \
Type value; \