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. --- .../protobuf/generated_message_reflection.cc | 26 +++++++++------------- 1 file changed, 10 insertions(+), 16 deletions(-) (limited to 'src/google/protobuf/generated_message_reflection.cc') diff --git a/src/google/protobuf/generated_message_reflection.cc b/src/google/protobuf/generated_message_reflection.cc index 247f772c..74ad00e7 100644 --- a/src/google/protobuf/generated_message_reflection.cc +++ b/src/google/protobuf/generated_message_reflection.cc @@ -2327,32 +2327,26 @@ class AssignDescriptorsHelper { // automatically delete the allocated reflection. MetadataOwner owns // all the allocated reflection instances. struct MetadataOwner { + ~MetadataOwner() { + for (auto range : metadata_arrays_) { + for (const Metadata* m = range.first; m < range.second; m++) { + delete m->reflection; + } + } + } + void AddArray(const Metadata* begin, const Metadata* end) { MutexLock lock(&mu_); metadata_arrays_.push_back(std::make_pair(begin, end)); } static MetadataOwner* Instance() { - static MetadataOwner* res = new MetadataOwner; + static MetadataOwner* res = OnShutdownDelete(new MetadataOwner); return res; } private: - // Use the constructor to register the shutdown code. Because c++ makes sure - // this called only once. - MetadataOwner() { OnShutdown(&DeleteMetadata); } - ~MetadataOwner() { - for (int i = 0; i < metadata_arrays_.size(); i++) { - for (const Metadata* m = metadata_arrays_[i].first; - m < metadata_arrays_[i].second; m++) { - delete m->reflection; - } - } - } - - static void DeleteMetadata() { - delete Instance(); - } + MetadataOwner() = default; // private because singleton Mutex mu_; std::vector > metadata_arrays_; -- cgit v1.2.3