From bd85f517798cf54796f0d4901c4f7f3d5963b354 Mon Sep 17 00:00:00 2001 From: Marianne Gagnon Date: Tue, 4 Aug 2015 19:26:12 -0400 Subject: [PATCH] Improve memory management in GP manager, fixes #2261 --- src/race/grand_prix_manager.cpp | 38 ++++++++++++++++++--------------- src/race/grand_prix_manager.hpp | 12 +++++++---- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/race/grand_prix_manager.cpp b/src/race/grand_prix_manager.cpp index 793874dc3..17d793fc7 100644 --- a/src/race/grand_prix_manager.cpp +++ b/src/race/grand_prix_manager.cpp @@ -39,8 +39,6 @@ GrandPrixManager::GrandPrixManager() // ---------------------------------------------------------------------------- GrandPrixManager::~GrandPrixManager() { - for(unsigned int i=0; igetId() == s.str()) + if (m_gp_data[i].getId() == s.str()) { unique = false; break; @@ -139,25 +138,31 @@ std::string GrandPrixManager::generateId() bool GrandPrixManager::existsName(const irr::core::stringw& name) const { for (unsigned int i = 0; i < m_gp_data.size(); i++) - if (m_gp_data[i]->getName() == name) + if (m_gp_data[i].getName() == name) return true; return false; } // existsName // ---------------------------------------------------------------------------- -GrandPrixData* GrandPrixManager::getGrandPrix(const std::string& s) const +const GrandPrixData* GrandPrixManager::getGrandPrix(const std::string& s) const { - return editGrandPrix(s); + for (unsigned int i = 0; igetId() == s) - return m_gp_data[i]; + if (m_gp_data[i].getId() == s) + return m_gp_data.get(i); } // for i in m_gp_data return NULL; @@ -166,13 +171,12 @@ GrandPrixData* GrandPrixManager::editGrandPrix(const std::string& s) const // ---------------------------------------------------------------------------- void GrandPrixManager::checkConsistency() { - for(unsigned int i=0; i= 0; i--) { - if(!m_gp_data[i]->checkConsistency()) + if (!m_gp_data[i].checkConsistency()) { // delete this GP, since a track is missing - delete *(m_gp_data.erase(m_gp_data.begin()+i)); - i--; + m_gp_data.erase(i); } } } // checkConsistency diff --git a/src/race/grand_prix_manager.hpp b/src/race/grand_prix_manager.hpp index 4159b5213..25092149e 100644 --- a/src/race/grand_prix_manager.hpp +++ b/src/race/grand_prix_manager.hpp @@ -23,6 +23,7 @@ #include #include +#include "utils/ptr_vector.hpp" #include "irrlicht.h" class GrandPrixData; @@ -35,7 +36,7 @@ class GrandPrixManager private: static const char* SUFFIX; - std::vector m_gp_data; + PtrVector m_gp_data; /** Load all the grands prix from the 3 directories known */ void loadFiles(); @@ -51,20 +52,23 @@ public: GrandPrixManager(); ~GrandPrixManager(); void reload(); - GrandPrixData* getGrandPrix(const std::string& s) const; bool existsName(const irr::core::stringw& name) const; void checkConsistency(); // Methods for the gp editor - GrandPrixData* editGrandPrix(const std::string& s) const; + GrandPrixData* editGrandPrix(const std::string& s); GrandPrixData* createNewGP(const irr::core::stringw& newName); GrandPrixData* copy(const std::string& id, const irr::core::stringw& newName); void remove(const std::string& id); // ------------------------------------------------------------------------ + /** Returns a pointer to the data for the specified GP. + * \param i Index of the GP. */ + const GrandPrixData* getGrandPrix(const std::string& s) const; + // ------------------------------------------------------------------------ /** Returns a pointer to the data for the specified GP. * \param i Index of the GP. */ - GrandPrixData* getGrandPrix(const int i) const { return m_gp_data[i]; } + const GrandPrixData* getGrandPrix(const int i) const { return m_gp_data.get(i); } // ------------------------------------------------------------------------ /** Returns the number of GPs. */ unsigned int getNumberOfGrandPrix() const { return (int)m_gp_data.size(); }