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/generated_message_util.cc | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'src/google/protobuf/generated_message_util.cc') diff --git a/src/google/protobuf/generated_message_util.cc b/src/google/protobuf/generated_message_util.cc index dac8ca90..e0241361 100644 --- a/src/google/protobuf/generated_message_util.cc +++ b/src/google/protobuf/generated_message_util.cc @@ -57,6 +57,12 @@ namespace google { namespace protobuf { namespace internal { +void DestroyMessage(const void* message) { + static_cast(message)->~MessageLite(); +} +void DestroyString(const void* s) { static_cast(s)->~string(); } + +ExplicitlyConstructed fixed_address_empty_string; double Infinity() { return std::numeric_limits::infinity(); @@ -65,14 +71,15 @@ double NaN() { return std::numeric_limits::quiet_NaN(); } -ExplicitlyConstructed<::std::string> fixed_address_empty_string; -GOOGLE_PROTOBUF_DECLARE_ONCE(empty_string_once_init_); - -void DeleteEmptyString() { fixed_address_empty_string.Destruct(); } - -void InitEmptyString() { +static bool InitProtobufDefaultsImpl() { fixed_address_empty_string.DefaultConstruct(); - OnShutdown(&DeleteEmptyString); + OnShutdownDestroyString(fixed_address_empty_string.get_mutable()); + return true; +} + +void InitProtobufDefaults() { + static bool is_inited = InitProtobufDefaultsImpl(); + (void)is_inited; } size_t StringSpaceUsedExcludingSelfLong(const string& str) { @@ -86,12 +93,6 @@ size_t StringSpaceUsedExcludingSelfLong(const string& str) { } } - - -void InitProtobufDefaults() { - GetEmptyString(); -} - template const T& Get(const void* ptr) { return *static_cast(ptr); -- cgit v1.2.3