From 6cb7a7be185926303c04ad897ac3f19f2f252cab Mon Sep 17 00:00:00 2001 From: Drew Jetter Date: Sat, 18 Jan 2014 16:39:50 -0700 Subject: [PATCH] Fixed bug #3489: The code assumed that two global variables would be constructed in a particular order, but global variable initialization order is compiler-dependent. (cherry picked from commit 6bf599b1bca8a58c7a656ddc169f7be0fc3094c6) --- modules/core/src/system.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/modules/core/src/system.cpp b/modules/core/src/system.cpp index 09daceed53..e881ac3f63 100644 --- a/modules/core/src/system.cpp +++ b/modules/core/src/system.cpp @@ -1129,17 +1129,24 @@ public: } } }; -static TLSContainerStorage tlsContainerStorage; + +// This is a wrapper function that will ensure 'tlsContainerStorage' is constructed on first use. +// For more information: http://www.parashift.com/c++-faq/static-init-order-on-first-use.html +static TLSContainerStorage& getTLSContainerStorage() +{ + static TLSContainerStorage *tlsContainerStorage = new TLSContainerStorage(); + return *tlsContainerStorage; +} TLSDataContainer::TLSDataContainer() : key_(-1) { - key_ = tlsContainerStorage.allocateKey(this); + key_ = getTLSContainerStorage().allocateKey(this); } TLSDataContainer::~TLSDataContainer() { - tlsContainerStorage.releaseKey(key_, this); + getTLSContainerStorage().releaseKey(key_, this); key_ = -1; } @@ -1164,7 +1171,7 @@ TLSStorage::~TLSStorage() void*& data = tlsData_[i]; if (data) { - tlsContainerStorage.destroyData(i, data); + getTLSContainerStorage().destroyData(i, data); data = NULL; } }