From a5aee1322f498537f8de83123099bbfcb2e3a969 Mon Sep 17 00:00:00 2001 From: Huynh Tran Date: Wed, 29 Jun 2005 10:28:36 +0000 Subject: Fixed memory leak and set conditional stats updates. --- src/account.cpp | 21 +++++++++------------ src/being.cpp | 9 +++++++++ src/dalstorage.cpp | 9 +++++++-- src/object.cpp | 7 ++++++- src/object.h | 5 +++-- 5 files changed, 34 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/account.cpp b/src/account.cpp index ed2c358e..97b723a0 100644 --- a/src/account.cpp +++ b/src/account.cpp @@ -70,18 +70,15 @@ Account::Account(const std::string& name, Account::~Account(void) throw() { - // we do not delete the Beings here because Storage keeps - // a list of Beings as well and the destructor of Storage - // takes care about it. - - /* - *for (Beings::iterator it = mCharacters.begin(); - * it != mCharacters.end(); - * ++it) - *{ - * delete (*it); - *} - */ + for (Beings::iterator it = mCharacters.begin(); + it != mCharacters.end(); + ++it) + { + if (*it != 0) { + delete (*it); + *it = 0; + } + } } diff --git a/src/being.cpp b/src/being.cpp index e6be76b3..d3df77f5 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -123,6 +123,7 @@ void Being::setStrength(const unsigned short strength) { mRawStats.strength = strength; + mNeedUpdate = true; } @@ -143,6 +144,7 @@ void Being::setAgility(const unsigned short agility) { mRawStats.agility = agility; + mNeedUpdate = true; } @@ -163,6 +165,7 @@ void Being::setVitality(const unsigned short vitality) { mRawStats.vitality = vitality; + mNeedUpdate = true; } @@ -183,6 +186,7 @@ void Being::setIntelligence(const unsigned short intelligence) { mRawStats.intelligence = intelligence; + mNeedUpdate = true; } @@ -205,6 +209,7 @@ void Being::setDexterity(const unsigned short dexterity) { mRawStats.dexterity = dexterity; + mNeedUpdate = true; } @@ -225,6 +230,7 @@ void Being::setLuck(const unsigned short luck) { mRawStats.luck = luck; + mNeedUpdate = true; } @@ -245,6 +251,7 @@ void Being::setRawStatistics(const RawStatistics& stats) { mRawStats = stats; + mNeedUpdate = true; } @@ -271,6 +278,8 @@ Being::update(void) mStats.magic = 10 + mRawStats.intelligence; mStats.accuracy = 50 + mRawStats.dexterity; mStats.speed = mRawStats.dexterity; + + mNeedUpdate = false; } diff --git a/src/dalstorage.cpp b/src/dalstorage.cpp index 7821e515..1970ed75 100644 --- a/src/dalstorage.cpp +++ b/src/dalstorage.cpp @@ -62,7 +62,9 @@ DALStorage::~DALStorage() it != mAccounts.end(); ++it) { - delete it->first; + if (it->first != 0) { + delete it->first; + } } // clean up characters. @@ -70,7 +72,10 @@ DALStorage::~DALStorage() it != mCharacters.end(); ++it) { - delete (*it); + if (*it != 0) { + delete (*it); + *it = 0; + } } } diff --git a/src/object.cpp b/src/object.cpp index e8dd18ca..2418f086 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -33,7 +33,8 @@ namespace tmwserv */ Object::Object(void) : mX(0), - mY(0) + mY(0), + mNeedUpdate(false) { mStats.health = 0; mStats.attack = 0; @@ -110,6 +111,10 @@ Object::setStatistics(const Statistics& stats) Statistics& Object::getStatistics(void) { + if (mNeedUpdate) { + update(); + } + return mStats; } diff --git a/src/object.h b/src/object.h index a696ea51..efd7a275 100644 --- a/src/object.h +++ b/src/object.h @@ -133,11 +133,12 @@ class Object protected: Statistics mStats; /**< stats modifiers or computed stats */ + bool mNeedUpdate; /**< update() must be invoked if true */ private: - unsigned int mX; /**< x coordinate */ - unsigned int mY; /**< y coordinate */ + unsigned int mX; /**< x coordinate */ + unsigned int mY; /**< y coordinate */ }; -- cgit v1.2.3-60-g2f50