From: Ingo Ruhnke Date: Tue, 12 Aug 2014 21:04:57 +0000 (+0200) Subject: Fixed memory leaks in TileManager X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=cc857db47ca82f4baf1ef00006524783291f5d42;p=supertux.git Fixed memory leaks in TileManager --- diff --git a/src/supertux/level.cpp b/src/supertux/level.cpp index 00d333579..b3644d6d6 100644 --- a/src/supertux/level.cpp +++ b/src/supertux/level.cpp @@ -77,7 +77,7 @@ Level::load(const std::string& filepath) const lisp::Lisp* tilesets_lisp = level->get_lisp("tilesets"); if(tilesets_lisp != NULL) { - tileset = tile_manager->parse_tileset_definition(*tilesets_lisp); + tileset = tile_manager->parse_tileset_definition(*tilesets_lisp).release(); free_tileset = true; } std::string tileset_name; diff --git a/src/supertux/resources.cpp b/src/supertux/resources.cpp index 2eac6a6d8..c29fa90bc 100644 --- a/src/supertux/resources.cpp +++ b/src/supertux/resources.cpp @@ -90,15 +90,9 @@ Resources::unload_shared() big_font.reset(); /* Free tilesets */ - if(tile_manager != NULL) - { - for(TileManager::TileSets::iterator it = tile_manager->tilesets.begin(); - it != tile_manager->tilesets.end(); ++it) - { - delete it->second; - it->second = NULL; - } - } + delete tile_manager; + tile_manager = 0; + if(sprite_manager != NULL) { delete sprite_manager; diff --git a/src/supertux/tile_manager.cpp b/src/supertux/tile_manager.cpp index 0b8ff046a..090b55c7e 100644 --- a/src/supertux/tile_manager.cpp +++ b/src/supertux/tile_manager.cpp @@ -31,21 +31,27 @@ TileManager::~TileManager() { } -TileSet* TileManager::get_tileset(const std::string &filename) +TileSet* +TileManager::get_tileset(const std::string &filename) { TileSets::const_iterator i = tilesets.find(filename); if(i != tilesets.end()) - return i->second; - - std::unique_ptr tileset (new TileSet(filename)); - tilesets.insert(std::make_pair(filename, tileset.get())); - - return tileset.release(); + { + return i->second.get(); + } + else + { + std::unique_ptr tileset(new TileSet(filename)); + TileSet* result = tileset.get(); + tilesets.insert(std::make_pair(filename, std::move(tileset))); + return result; + } } -TileSet* TileManager::parse_tileset_definition(const Reader& reader) +std::unique_ptr +TileManager::parse_tileset_definition(const Reader& reader) { - std::unique_ptr result(new TileSet()); + std::unique_ptr result(new TileSet); lisp::ListIterator iter(&reader); while(iter.next()) { @@ -74,7 +80,7 @@ TileSet* TileManager::parse_tileset_definition(const Reader& reader) result->merge(tileset, start, end, offset); } - return result.release(); + return std::move(result); } /* EOF */ diff --git a/src/supertux/tile_manager.hpp b/src/supertux/tile_manager.hpp index 5d1317ea2..6a5fcee5e 100644 --- a/src/supertux/tile_manager.hpp +++ b/src/supertux/tile_manager.hpp @@ -18,6 +18,7 @@ #define HEADER_SUPERTUX_SUPERTUX_TILE_MANAGER_HPP #include +#include #include #include "util/reader_fwd.hpp" @@ -26,9 +27,8 @@ class TileSet; class TileManager { - friend class Resources; private: - typedef std::map TileSets; + typedef std::map > TileSets; TileSets tilesets; public: @@ -37,7 +37,7 @@ public: TileSet* get_tileset(const std::string &filename); - TileSet* parse_tileset_definition(const Reader& reader); + std::unique_ptr parse_tileset_definition(const Reader& reader); }; #endif diff --git a/src/worldmap/worldmap.cpp b/src/worldmap/worldmap.cpp index 3bda83784..dfc3bfc37 100644 --- a/src/worldmap/worldmap.cpp +++ b/src/worldmap/worldmap.cpp @@ -264,7 +264,7 @@ WorldMap::load(const std::string& filename) const lisp::Lisp* tilesets_lisp = level->get_lisp("tilesets"); if(tilesets_lisp != NULL) { - tileset = tile_manager->parse_tileset_definition(*tilesets_lisp); + tileset = tile_manager->parse_tileset_definition(*tilesets_lisp).release(); free_tileset = true; } std::string tileset_name;