From b6741865f2bb541699d04f128a5389d7b8a4babe Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 20 Oct 2013 14:16:21 +0200 Subject: [PATCH] Fixed memory leak in cMobFamilyCollecter. --- source/MobFamilyCollecter.cpp | 27 +-------------------------- source/MobFamilyCollecter.h | 14 ++------------ source/World.cpp | 32 ++++++++++++++++++-------------- 3 files changed, 21 insertions(+), 52 deletions(-) diff --git a/source/MobFamilyCollecter.cpp b/source/MobFamilyCollecter.cpp index 086fa5f40..e9c69e078 100644 --- a/source/MobFamilyCollecter.cpp +++ b/source/MobFamilyCollecter.cpp @@ -6,32 +6,7 @@ -cMobFamilyCollecter::tMobFamilyList cMobFamilyCollecter::InitMobFamilyBeforeCx11(void) -{ - std::set toReturn; - toReturn.insert(cMonster::mfHostile); - toReturn.insert(cMonster::mfPassive); - toReturn.insert(cMonster::mfAmbient); - toReturn.insert(cMonster::mfWater); - return toReturn; -} - - - - - -cMobFamilyCollecter::tMobFamilyList & cMobFamilyCollecter::m_AllFamilies(void) -{ - // TODO: This memory is leaked: - static tMobFamilyList * AllFamilies = new tMobFamilyList(InitMobFamilyBeforeCx11()); - return *AllFamilies; -} - - - - - -void cMobFamilyCollecter::CollectMob(cMonster& a_Monster) +void cMobFamilyCollecter::CollectMob(cMonster & a_Monster) { cMonster::eFamily MobFamily = a_Monster.GetMobFamily(); m_Mobs[MobFamily].insert(&a_Monster); diff --git a/source/MobFamilyCollecter.h b/source/MobFamilyCollecter.h index cd05b6adb..6cef133b5 100644 --- a/source/MobFamilyCollecter.h +++ b/source/MobFamilyCollecter.h @@ -16,16 +16,12 @@ class cChunk; -/** This class is used to collect, for each Mob, what is it's family. It was first -being designed to check the caps of the mobs (no more than ... hostile mob in the world) - -as side effects : it also know what is the spawnrate of each family : MG TODO relocate +/** This class is used to collect the list of mobs for each family */ class cMobFamilyCollecter { public : typedef const std::set tMobFamilyList; - typedef const std::map tMobSpawRate; // collect a mob void CollectMob(cMonster & a_Monster); @@ -33,15 +29,9 @@ public : // return the number of mobs for this family int GetNumberOfCollectedMobs(cMonster::eFamily a_Family); - static tMobFamilyList & m_AllFamilies(void); - - static tMobSpawRate & m_SpawnRate(void); - protected : - std::map > m_Mobs; + std::map > m_Mobs; - static tMobFamilyList InitMobFamilyBeforeCx11(void); - static tMobSpawRate InitMobSpawnRateBeforeCx11(void); } ; diff --git a/source/World.cpp b/source/World.cpp index dad36ead4..d1ddb0e6e 100644 --- a/source/World.cpp +++ b/source/World.cpp @@ -736,38 +736,42 @@ void cWorld::TickWeather(float a_Dt) void cWorld::TickMobs(float a_Dt) { - if (!m_bAnimals) - { - return; - } - - // before every Mob action, we have to "counts" them depending on the distance to players, on their megatype ... + // before every Mob action, we have to count them depending on the distance to players, on their family ... cMobCensus MobCensus; m_ChunkMap->CollectMobCensus(MobCensus); if (m_bAnimals) { - for (cMobFamilyCollecter::tMobFamilyList::const_iterator itr = cMobFamilyCollecter::m_AllFamilies().begin(); itr != cMobFamilyCollecter::m_AllFamilies().end(); itr++) + // Spawning is enabled, spawn now: + static const cMonster::eFamily AllFamilies[] = { - int spawnrate = cMonster::GetSpawnRate(*itr); + cMonster::mfHostile, + cMonster::mfPassive, + cMonster::mfAmbient, + cMonster::mfWater, + } ; + for (int i = 0; i < ARRAYCOUNT(AllFamilies); i++) + { + cMonster::eFamily Family = AllFamilies[i]; + int spawnrate = cMonster::GetSpawnRate(Family); if ( - (m_LastSpawnMonster[*itr] > m_WorldAge - spawnrate) || // Not reached the needed tiks before the next round - MobCensus.IsCapped(*itr) + (m_LastSpawnMonster[Family] > m_WorldAge - spawnrate) || // Not reached the needed tiks before the next round + MobCensus.IsCapped(Family) ) { continue; } - m_LastSpawnMonster[*itr] = m_WorldAge; - cMobSpawner Spawner(*itr, m_AllowedMobs); + m_LastSpawnMonster[Family] = m_WorldAge; + cMobSpawner Spawner(Family, m_AllowedMobs); if (Spawner.CanSpawnAnything()) { m_ChunkMap->SpawnMobs(Spawner); // do the spawn - for(cMobSpawner::tSpawnedContainer::const_iterator itr2 = Spawner.getSpawned().begin(); itr2 != Spawner.getSpawned().end(); itr2++) + for (cMobSpawner::tSpawnedContainer::const_iterator itr2 = Spawner.getSpawned().begin(); itr2 != Spawner.getSpawned().end(); itr2++) { SpawnMobFinalize(*itr2); } } - } // for itr - Families[] + } // for i - AllFamilies[] } // if (Spawning enabled) // move close mobs