diff options
author | Adam Cozzette <acozzette@google.com> | 2018-07-06 14:12:33 -0700 |
---|---|---|
committer | Adam Cozzette <acozzette@google.com> | 2018-07-06 15:02:40 -0700 |
commit | a9abc7831e45257d334cfa682746b6cadf9e95d9 (patch) | |
tree | fdb892e4e34269f6b3bbb6f629a912d4c63c2d70 /src/google/protobuf/generated_message_util.cc | |
parent | f7ada1280fac4af717d478e6a9765d3f02b418b3 (diff) | |
download | protobuf-a9abc7831e45257d334cfa682746b6cadf9e95d9.tar.gz protobuf-a9abc7831e45257d334cfa682746b6cadf9e95d9.tar.bz2 protobuf-a9abc7831e45257d334cfa682746b6cadf9e95d9.zip |
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.
Diffstat (limited to 'src/google/protobuf/generated_message_util.cc')
-rw-r--r-- | src/google/protobuf/generated_message_util.cc | 27 |
1 files changed, 14 insertions, 13 deletions
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<const MessageLite*>(message)->~MessageLite(); +} +void DestroyString(const void* s) { static_cast<const string*>(s)->~string(); } + +ExplicitlyConstructed<std::string> fixed_address_empty_string; double Infinity() { return std::numeric_limits<double>::infinity(); @@ -65,14 +71,15 @@ double NaN() { return std::numeric_limits<double>::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 <typename T> const T& Get(const void* ptr) { return *static_cast<const T*>(ptr); |