From: Tim Goya Date: Sun, 7 Oct 2007 19:25:34 +0000 (+0000) Subject: Fix minor memory leaks X-Git-Url: https://git.octo.it/?a=commitdiff_plain;h=1c9e52d43a80d1f3ec5aadbb04ef1e14bb775acb;p=supertux.git Fix minor memory leaks SVN-Revision: 5163 --- diff --git a/src/game_session.cpp b/src/game_session.cpp index f1b610814..0a61cd336 100644 --- a/src/game_session.cpp +++ b/src/game_session.cpp @@ -166,6 +166,7 @@ GameSession::~GameSession() delete capture_demo_stream; delete playback_demo_stream; delete demo_controller; + free_options_menu(); current_ = NULL; } diff --git a/src/main.cpp b/src/main.cpp index ade5d5d8d..84160c7f0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -582,7 +582,6 @@ int main(int argc, char** argv) delete main_loop; main_loop = NULL; - free_options_menu(); unload_shared(); quit_audio(); diff --git a/src/object_factory.cpp b/src/object_factory.cpp index 533514059..fe89724c4 100644 --- a/src/object_factory.cpp +++ b/src/object_factory.cpp @@ -27,12 +27,10 @@ #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()); diff --git a/src/object_factory.hpp b/src/object_factory.hpp index a6e431051..0e0d0d350 100644 --- a/src/object_factory.hpp +++ b/src/object_factory.hpp @@ -37,10 +37,14 @@ public: * Remember to delete the objects later */ virtual GameObject* create_object(const lisp::Lisp& reader) = 0; -}; -typedef std::map Factories; -extern Factories* object_factories; + typedef std::map 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) \ diff --git a/src/options_menu.cpp b/src/options_menu.cpp index c4c7ac9a6..93ab43d34 100644 --- a/src/options_menu.cpp +++ b/src/options_menu.cpp @@ -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; }