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_reflection.cc | |
parent | f7ada1280fac4af717d478e6a9765d3f02b418b3 (diff) |
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_reflection.cc')
-rw-r--r-- | src/google/protobuf/generated_message_reflection.cc | 26 |
1 files changed, 10 insertions, 16 deletions
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<std::pair<const Metadata*, const Metadata*> > metadata_arrays_; |