Fix minor memory leaks
authorTim Goya <tuxdev103@gmail.com>
Sun, 7 Oct 2007 19:25:34 +0000 (19:25 +0000)
committerTim Goya <tuxdev103@gmail.com>
Sun, 7 Oct 2007 19:25:34 +0000 (19:25 +0000)
SVN-Revision: 5163

src/game_session.cpp
src/main.cpp
src/object_factory.cpp
src/object_factory.hpp
src/options_menu.cpp

index f1b6108..0a61cd3 100644 (file)
@@ -166,6 +166,7 @@ GameSession::~GameSession()
   delete capture_demo_stream;
   delete playback_demo_stream;
   delete demo_controller;
+  free_options_menu();
 
   current_ = NULL;
 }
index ade5d5d..84160c7 100644 (file)
@@ -582,7 +582,6 @@ int main(int argc, char** argv)
   delete main_loop;
   main_loop = NULL;
 
-  free_options_menu();
   unload_shared();
   quit_audio();
 
index 5335140..fe89724 100644 (file)
 #include "object_factory.hpp"
 #include "math/vector.hpp"
 
-Factories* object_factories = 0;
-
 GameObject* create_object(const std::string& name, const lisp::Lisp& reader)
 {
-  Factories::iterator i = object_factories->find(name);
-  if(i == object_factories->end()) {
+  Factory::Factories::iterator i = Factory::get_factories().find(name);
+  if(i == Factory::get_factories().end()) {
     std::stringstream msg;
     msg << "No factory for object '" << name << "' found.";
     throw std::runtime_error(msg.str());
index a6e4310..0e0d0d3 100644 (file)
@@ -37,10 +37,14 @@ public:
    * Remember to delete the objects later
    */
   virtual GameObject* create_object(const lisp::Lisp& reader) = 0;
-};
 
-typedef std::map<std::string, Factory*> Factories;
-extern Factories* object_factories;
+  typedef std::map<std::string, Factory*> Factories;
+  static Factories &get_factories()
+  {
+    static Factories object_factories;
+    return object_factories;
+  }
+};
 
 GameObject* create_object(const std::string& name, const lisp::Lisp& reader);
 GameObject* create_object(const std::string& name, const Vector& pos);
@@ -57,10 +61,12 @@ class INTERN_##CLASS##Factory : public Factory                    \
 public:                                                           \
   INTERN_##CLASS##Factory()                                       \
   {                                                               \
-    if(object_factories == 0)                                     \
-      object_factories = new Factories;                           \
+    get_factories()[NAME] = this;                                \
+  }                                                               \
                                                                   \
-    object_factories->insert(std::make_pair(NAME, this));         \
+  ~INTERN_##CLASS##Factory()                                      \
+  {                                                               \
+    get_factories().erase(NAME);                                 \
   }                                                               \
                                                                   \
   virtual GameObject* create_object(const lisp::Lisp& reader)     \
index c4c7ac9..93ab43d 100644 (file)
@@ -158,14 +158,13 @@ OptionsMenu::menu_action(MenuItem* item)
 
 Menu* get_options_menu()
 {
-  //if(options_menu == NULL)
+  //static OptionsMenu menu;
   options_menu = new OptionsMenu();
-
   return options_menu;
 }
 
 void free_options_menu()
 {
   delete options_menu;
-  options_menu = NULL;
+  options_menu = 0;
 }