Commits

Anonymous committed 46c510f

Make sure GeneratedMessageFactory is deleted on shutdown.

  • Participants
  • Parent commits 5dcd292

Comments (0)

Files changed (1)

File src/google/protobuf/message.cc

 #include <google/protobuf/message.h>
 
 #include <google/protobuf/stubs/common.h>
+#include <google/protobuf/stubs/once.h>
 #include <google/protobuf/io/coded_stream.h>
 #include <google/protobuf/io/zero_copy_stream_impl.h>
 #include <google/protobuf/descriptor.pb.h>
   hash_map<const Descriptor*, const Message*> 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() {
-  // No need for thread-safety here because this will be called at static
-  // initialization time.  (And GCC4 makes this thread-safe anyway.)
-  static GeneratedMessageFactory* singleton = new GeneratedMessageFactory;
-  return singleton;
+  GoogleOnceInit(&generated_message_factory_once_init_,
+                 &InitGeneratedMessageFactory);
+  return generated_message_factory_;
 }
 
 void GeneratedMessageFactory::RegisterFile(