From adea360c7ae8cd3166d00b25796c7b2b00800cb4 Mon Sep 17 00:00:00 2001 From: Igor Date: Fri, 16 Sep 2016 20:18:49 +0300 Subject: [PATCH 1/5] Use statics initialization on demand fixed #331 Fixed crash CEREAL_THREAD_SAFE=1 with polymorphic class. Crash happened because StaticObject::instanceMutex not initialized when StaticObject::lock() called for the first time. Basically it is better to use static vars inside static functions to have predefined order of construction like it is done in StaticObject::getInstance(), which calls create and forces instantiation at pre-execution time. Also in this commit removed static T &instance; since it is possible that there were be two instances created 1) template T & StaticObject::instance = StaticObject::create(); 2) inside StaticObject::create() --- include/cereal/details/static_object.hpp | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/include/cereal/details/static_object.hpp b/include/cereal/details/static_object.hpp index 6afdb75d1..b40694081 100644 --- a/include/cereal/details/static_object.hpp +++ b/include/cereal/details/static_object.hpp @@ -73,7 +73,7 @@ namespace cereal static T & create() { static T t; - instantiate(instance); + instantiate(t); return t; } @@ -109,23 +109,15 @@ namespace cereal static LockGuard lock() { #if CEREAL_THREAD_SAFE + std::mutex instanceMutex; return LockGuard{instanceMutex}; #else return LockGuard{}; #endif } - private: - static T & instance; - #if CEREAL_THREAD_SAFE - static std::mutex instanceMutex; - #endif }; - template T & StaticObject::instance = StaticObject::create(); - #if CEREAL_THREAD_SAFE - template std::mutex StaticObject::instanceMutex; - #endif } // namespace detail } // namespace cereal From e715149f4dd18890d0abc36f5cd5df3108ac6810 Mon Sep 17 00:00:00 2001 From: Igor Date: Wed, 12 Oct 2016 10:35:38 +0300 Subject: [PATCH 2/5] Update static_object.hpp fixed misprint --- include/cereal/details/static_object.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/cereal/details/static_object.hpp b/include/cereal/details/static_object.hpp index b40694081..41ffdcc09 100644 --- a/include/cereal/details/static_object.hpp +++ b/include/cereal/details/static_object.hpp @@ -109,7 +109,7 @@ namespace cereal static LockGuard lock() { #if CEREAL_THREAD_SAFE - std::mutex instanceMutex; + static std::mutex instanceMutex; return LockGuard{instanceMutex}; #else return LockGuard{}; From 500e7a0188c03db64c671c9cd7502b3258cb373a Mon Sep 17 00:00:00 2001 From: Igor Date: Wed, 12 Oct 2016 10:45:37 +0300 Subject: [PATCH 3/5] Update static_object.hpp I cannot understand what is going on here with `static T & instance;`, I propose to do not change `create()`. But it would be better if someone explain what is happening here :) --- include/cereal/details/static_object.hpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/include/cereal/details/static_object.hpp b/include/cereal/details/static_object.hpp index 41ffdcc09..2624c1083 100644 --- a/include/cereal/details/static_object.hpp +++ b/include/cereal/details/static_object.hpp @@ -73,7 +73,7 @@ namespace cereal static T & create() { static T t; - instantiate(t); + instantiate(instance); return t; } @@ -115,9 +115,12 @@ namespace cereal return LockGuard{}; #endif } - + private: + static T & instance; }; + template T & StaticObject::instance = StaticObject::create(); + } // namespace detail } // namespace cereal From 74e0b1b698472b69ae2df9489984ea1d367058b7 Mon Sep 17 00:00:00 2001 From: Igor Date: Wed, 12 Oct 2016 10:47:01 +0300 Subject: [PATCH 4/5] Update static_object.hpp removed empty extra line --- include/cereal/details/static_object.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/include/cereal/details/static_object.hpp b/include/cereal/details/static_object.hpp index 2624c1083..a904d4538 100644 --- a/include/cereal/details/static_object.hpp +++ b/include/cereal/details/static_object.hpp @@ -120,7 +120,6 @@ namespace cereal }; template T & StaticObject::instance = StaticObject::create(); - } // namespace detail } // namespace cereal From 96deb7e9d30feacd7ddfe8647ffd71e9f55d625e Mon Sep 17 00:00:00 2001 From: Igor Date: Wed, 12 Oct 2016 10:47:33 +0300 Subject: [PATCH 5/5] Update static_object.hpp --- include/cereal/details/static_object.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/include/cereal/details/static_object.hpp b/include/cereal/details/static_object.hpp index a904d4538..b232880c7 100644 --- a/include/cereal/details/static_object.hpp +++ b/include/cereal/details/static_object.hpp @@ -115,6 +115,7 @@ namespace cereal return LockGuard{}; #endif } + private: static T & instance; };