From 4a43cb1c6e5ba304d70df7066c61c52718a5f249 Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Wed, 5 Oct 2005 21:45:27 +0000 Subject: Fixed some resource cleanup and memory leaks. Also changed the way dangling references to resources are reported to be more informative. --- src/engine.cpp | 6 ++++- src/gui/button.cpp | 15 +++++++------ src/gui/button.h | 2 +- src/gui/char_select.cpp | 2 ++ src/gui/login.cpp | 47 ++++++++++++++++++++++----------------- src/gui/skill.cpp | 1 + src/main.cpp | 3 +++ src/net/messagein.cpp | 2 +- src/resources/resource.h | 6 +++++ src/resources/resourcemanager.cpp | 15 +++++++------ 10 files changed, 61 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/engine.cpp b/src/engine.cpp index c5bd9f10..1dd7c752 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -165,12 +165,16 @@ Engine::Engine(): weaponset = new Spriteset(weaponbitmap, 160, 120); itemset = new Spriteset(itembitmap, 32, 32); + npcbmp->decRef(); + emotionbmp->decRef(); + weaponbitmap->decRef(); + itembitmap->decRef(); + attackTarget = resman->getImage("graphics/gui/attack_target.png"); if (!attackTarget) logger->error("Unable to load attack_target.png"); // Initialize item manager itemDb = new ItemManager(); - } Engine::~Engine() diff --git a/src/gui/button.cpp b/src/gui/button.cpp index b2468036..bf2ca6c6 100644 --- a/src/gui/button.cpp +++ b/src/gui/button.cpp @@ -35,14 +35,14 @@ #include "../resources/resourcemanager.h" ImageRect Button::button[4]; -int Button::instances = 0; +int Button::mInstances = 0; Button::Button(const std::string& caption): gcn::Button(caption) { setBorderSize(0); - if (instances == 0) + if (mInstances == 0) { // Load the skin ResourceManager *resman = ResourceManager::getInstance(); @@ -71,14 +71,14 @@ Button::Button(const std::string& caption): } } - instances++; + mInstances++; } Button::~Button() { - instances--; + mInstances--; - if (instances == 0) + if (mInstances == 0) { int a, x, y, mode; @@ -112,8 +112,9 @@ void Button::draw(gcn::Graphics* graphics) mode = 0; } - dynamic_cast(graphics)->drawImageRect(0, 0, getWidth(), getHeight(), - button[mode]); + dynamic_cast(graphics)->drawImageRect(0, 0, + getWidth(), getHeight(), + button[mode]); graphics->setColor(getForegroundColor()); diff --git a/src/gui/button.h b/src/gui/button.h index 8873358d..86467769 100644 --- a/src/gui/button.h +++ b/src/gui/button.h @@ -54,7 +54,7 @@ class Button : public gcn::Button { private: static ImageRect button[4]; /**< Button state graphics */ - static int instances; /**< Number of button instances */ + static int mInstances; /**< Number of button instances */ }; #endif diff --git a/src/gui/char_select.cpp b/src/gui/char_select.cpp index 84e0e13a..b7258aba 100644 --- a/src/gui/char_select.cpp +++ b/src/gui/char_select.cpp @@ -370,6 +370,8 @@ CharCreateDialog::~CharCreateDialog() delete nextHairStyleButton; delete prevHairStyleButton; delete hairStyleLabel; + delete createButton; + delete cancelButton; delete playerBox; } diff --git a/src/gui/login.cpp b/src/gui/login.cpp index b4d431d2..aea77768 100644 --- a/src/gui/login.cpp +++ b/src/gui/login.cpp @@ -51,13 +51,14 @@ SERVER_INFO **server_info; WrongUsernameNoticeListener wrongUsernameNoticeListener; WrongPasswordNoticeListener wrongPasswordNoticeListener; -void WrongPasswordNoticeListener::setLoginDialog( - LoginDialog *loginDialog) +void +WrongPasswordNoticeListener::setLoginDialog(LoginDialog *loginDialog) { mLoginDialog = loginDialog; } -void WrongPasswordNoticeListener::action(const std::string &eventId) +void +WrongPasswordNoticeListener::action(const std::string &eventId) { // Reset the password and put the caret ready to retype it. mLoginDialog->passField->setText(""); @@ -66,13 +67,14 @@ void WrongPasswordNoticeListener::action(const std::string &eventId) wrongLoginNotice = NULL; } -void WrongUsernameNoticeListener::setLoginDialog( - LoginDialog *loginDialog) +void +WrongUsernameNoticeListener::setLoginDialog(LoginDialog *loginDialog) { mLoginDialog = loginDialog; } -void WrongUsernameNoticeListener::action(const std::string &eventId) +void +WrongUsernameNoticeListener::action(const std::string &eventId) { // Set the focus on the username Field mLoginDialog->userField->setCaretPosition(LEN_MAX_USERNAME - 1); @@ -175,7 +177,8 @@ LoginDialog::~LoginDialog() delete registerButton; } -void LoginDialog::action(const std::string& eventId) +void +LoginDialog::action(const std::string& eventId) { if (eventId == "ok") { @@ -267,9 +270,9 @@ void LoginDialog::action(const std::string& eventId) { // Name too short std::stringstream errorMessage; - errorMessage << "The username needs to be at least "; - errorMessage << LEN_MIN_USERNAME; - errorMessage << " characters long."; + errorMessage << "The username needs to be at least " + << LEN_MIN_USERNAME + << " characters long."; wrongLoginNotice = new OkDialog("Error", errorMessage.str(), &wrongUsernameNoticeListener); } @@ -277,9 +280,9 @@ void LoginDialog::action(const std::string& eventId) { // Name too long std::stringstream errorMessage; - errorMessage << "The username needs to be less than "; - errorMessage << LEN_MAX_USERNAME; - errorMessage << " characters long."; + errorMessage << "The username needs to be less than " + << LEN_MAX_USERNAME + << " characters long."; wrongLoginNotice = new OkDialog("Error", errorMessage.str(), &wrongUsernameNoticeListener); } @@ -287,9 +290,9 @@ void LoginDialog::action(const std::string& eventId) { // Pass too short std::stringstream errorMessage; - errorMessage << "The password needs to be at least "; - errorMessage << LEN_MIN_PASSWORD; - errorMessage << " characters long."; + errorMessage << "The password needs to be at least " + << LEN_MIN_PASSWORD + << " characters long."; wrongLoginNotice = new OkDialog("Error", errorMessage.str(), &wrongPasswordNoticeListener); } @@ -297,9 +300,9 @@ void LoginDialog::action(const std::string& eventId) { // Pass too long std::stringstream errorMessage; - errorMessage << "The password needs to be less than "; - errorMessage << LEN_MAX_PASSWORD; - errorMessage << " characters long."; + errorMessage << "The password needs to be less than " + << LEN_MAX_PASSWORD + << " characters long."; wrongLoginNotice = new OkDialog("Error", errorMessage.str(), &wrongPasswordNoticeListener); } @@ -311,7 +314,8 @@ void LoginDialog::action(const std::string& eventId) } } -void loginInputHandler(SDL_KeyboardEvent *keyEvent) +void +loginInputHandler(SDL_KeyboardEvent *keyEvent) { if (keyEvent->keysym.sym == SDLK_ESCAPE) { @@ -319,7 +323,8 @@ void loginInputHandler(SDL_KeyboardEvent *keyEvent) } } -int attemptLogin(const std::string& user, const std::string& pass) +int +attemptLogin(const std::string& user, const std::string& pass) { int ret; diff --git a/src/gui/skill.cpp b/src/gui/skill.cpp index a52cd267..75f77e39 100644 --- a/src/gui/skill.cpp +++ b/src/gui/skill.cpp @@ -115,6 +115,7 @@ SkillDialog::~SkillDialog() delete pointsLabel; delete incButton; delete closeButton; + delete useButton; for (int i = skillList.size() - 1; i >= 0; i--) { diff --git a/src/main.cpp b/src/main.cpp index a6fef07c..31a1b232 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -271,6 +271,9 @@ void init_engine() playerset = new Spriteset(playerImg, 64, 64); hairset = new Spriteset(hairImg, 40, 40); + playerImg->decRef(); + hairImg->decRef(); + gui = new Gui(graphics); state = UPDATE_STATE; /**< Initial game state */ diff --git a/src/net/messagein.cpp b/src/net/messagein.cpp index e3f8c28e..6086e096 100644 --- a/src/net/messagein.cpp +++ b/src/net/messagein.cpp @@ -145,7 +145,7 @@ MessageIn::readString(int length) mPos += stringLength; readString = tmpString; - delete tmpString; + delete[] tmpString; return readString; } diff --git a/src/resources/resource.h b/src/resources/resource.h index ce3a62fb..fe534606 100644 --- a/src/resources/resource.h +++ b/src/resources/resource.h @@ -53,6 +53,12 @@ class Resource bool decRef(); + /** + * Return the path identifying this resource. + */ + const std::string& + getIdPath() { return mIdPath; } + protected: /** * Destructor. diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp index c941e16e..4a8a010a 100644 --- a/src/resources/resourcemanager.cpp +++ b/src/resources/resourcemanager.cpp @@ -31,6 +31,7 @@ #include "soundeffect.h" #include "../log.h" +#include "../gui/button.h" ResourceManager *ResourceManager::instance = NULL; @@ -43,23 +44,23 @@ ResourceManager::~ResourceManager() { // Create our resource iterator. std::map::iterator iter = resources.begin(); - int danglingResources = 0; - int danglingReferences = 0; // Iterate through and release references until objects are deleted. while (!resources.empty()) { Resource *res = resources.begin()->second; - danglingResources++; + std::string id = res->getIdPath(); + + int references = 0; do { - danglingReferences++; + references++; } while (!res->decRef()); - } - logger->log("ResourceManager::~ResourceManager() cleaned up %d references " - "to %d resources", danglingReferences, danglingResources); + logger->log("ResourceManager::~ResourceManager() cleaned up %d " + "references to %s", references, id.c_str()); + } } bool -- cgit v1.2.3-70-g09d2