From a9abc7831e45257d334cfa682746b6cadf9e95d9 Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Fri, 6 Jul 2018 14:12:33 -0700 Subject: Fix initialization with Visual Studio It appears that Visual Studio does not work well with std::once_flag because it has a bug causing it to initialize that during dynamic initialization instead of constant initialization. This change works around the problem by using function static initializers instead. @gerben-s originally wrote this change for the Google-internal codebase but I am just cherry-picking it here. This fixes #4773. --- src/google/protobuf/message.cc | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) (limited to 'src/google/protobuf/message.cc') diff --git a/src/google/protobuf/message.cc b/src/google/protobuf/message.cc index 810db233..9b758080 100644 --- a/src/google/protobuf/message.cc +++ b/src/google/protobuf/message.cc @@ -262,9 +262,6 @@ namespace { class GeneratedMessageFactory : public MessageFactory { public: - GeneratedMessageFactory(); - ~GeneratedMessageFactory(); - static GeneratedMessageFactory* singleton(); typedef void RegistrationFunc(const string&); @@ -284,25 +281,9 @@ class GeneratedMessageFactory : public MessageFactory { hash_map type_map_; }; -GeneratedMessageFactory* generated_message_factory_ = NULL; -GOOGLE_PROTOBUF_DECLARE_ONCE(generated_message_factory_once_init_); - -void ShutdownGeneratedMessageFactory() { - delete generated_message_factory_; -} - -void InitGeneratedMessageFactory() { - generated_message_factory_ = new GeneratedMessageFactory; - internal::OnShutdown(&ShutdownGeneratedMessageFactory); -} - -GeneratedMessageFactory::GeneratedMessageFactory() {} -GeneratedMessageFactory::~GeneratedMessageFactory() {} - GeneratedMessageFactory* GeneratedMessageFactory::singleton() { - ::google::protobuf::GoogleOnceInit(&generated_message_factory_once_init_, - &InitGeneratedMessageFactory); - return generated_message_factory_; + static auto instance = internal::OnShutdownDelete(new GeneratedMessageFactory); + return instance; } void GeneratedMessageFactory::RegisterFile( -- cgit v1.2.3