Packets: Fix global registry destruction segfault
g0dil [Wed, 15 Sep 2010 15:32:25 +0000 (15:32 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1717 270642c3-0616-0410-b53a-bc976706d245

senf/Packets/PacketRegistry.ct
senf/Packets/PacketRegistry.cti
senf/Packets/PacketRegistry.hh

index 6237734..b2c6501 100644 (file)
@@ -86,17 +86,6 @@ senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::type()
 }
 
 ///////////////////////////////////////////////////////////////////////////
-// senf::PacketRegistry<Tag>
-
-template <class Tag>
-prefix_ typename senf::PacketRegistry<Tag>::Registry &
-senf::PacketRegistry<Tag>::registry()
-{
-    static Registry registry (prettyName(typeid(Tag)));
-    return registry;
-}
-
-///////////////////////////////////////////////////////////////////////////
 // senf::detail::PacketRegistryImpl<KeyType>:
 
 template <class KeyType>
index b70af2a..44ee816 100644 (file)
@@ -66,13 +66,15 @@ template <class Tag>
 template <class PacketType>
 prefix_ void senf::PacketRegistry<Tag>::unregisterPacket()
 {
-    registry().unregisterPacket<PacketType>();
+    if (alive())
+        registry().unregisterPacket<PacketType>();
 }
 
 template <class Tag>
 prefix_ void senf::PacketRegistry<Tag>::unregisterPacket(typename Tag::key_t key, int priority)
 {
-    registry().unregisterPacket(key, priority);
+    if (alive())
+        registry().unregisterPacket(key, priority);
 }
 
 template <class Tag>
@@ -128,6 +130,18 @@ prefix_ typename senf::PacketRegistry<Tag>::iterator senf::PacketRegistry<Tag>::
     return registry().end();
 }
 
+template <class Tag>
+prefix_ senf::PacketRegistry<Tag>::PacketRegistry()
+    : registry_ (prettyName(typeid(Tag)))
+{}
+
+template <class Tag>
+prefix_ typename senf::PacketRegistry<Tag>::Registry &
+senf::PacketRegistry<Tag>::registry()
+{
+    return instance().registry_;
+}
+
 ///////////////////////////////////////////////////////////////////////////
 // senf::detail::PacketRegistryImpl<KeyType>
 
index 868d485..1f748a1 100644 (file)
@@ -32,6 +32,7 @@
 #include <boost/optional.hpp>
 #include <boost/preprocessor/cat.hpp>
 #include <senf/Utils/Exception.hh>
+#include <senf/Utils/singleton.hh>
 #include "Packet.hh"
 
 #include "PacketRegistry.ih"
@@ -86,6 +87,7 @@ namespace senf {
      */
     template <class Tag>
     class PacketRegistry
+        : private senf::singleton< PacketRegistry<Tag> >
     {
     public:
         typedef typename detail::PacketRegistryImpl<typename Tag::key_t>::iterator iterator;
@@ -219,8 +221,16 @@ namespace senf {
         static iterator end();
 
     private:
+        using singleton<PacketRegistry>::instance;
+        using singleton<PacketRegistry>::alive;
+
+        PacketRegistry();
+
         typedef detail::PacketRegistryImpl<typename Tag::key_t> Registry;
         static Registry & registry();
+        Registry registry_;
+
+        friend class senf::singleton< PacketRegistry >;
     };
 
     /** \brief Statically add an entry to a packet registry