From: kainazzzo Date: Sun, 2 Apr 2006 03:34:01 +0000 (+0000) Subject: fixed a ton of memory leaks X-Git-Url: https://jfr.im/git/irc/gameservirc.git/commitdiff_plain/5de7a0b046cca5af124ec6332d16f53dfb9b3af7?hp=9c443e07ade40b3deb3e83d9fb651fb0490d2664 fixed a ton of memory leaks git-svn-id: https://svn.code.sf.net/p/gameservirc/code/trunk@439 bc333340-6410-0410-a689-9d09f3c113fa --- diff --git a/gameserv/Changes b/gameserv/Changes index 133d629..37a7e8a 100644 --- a/gameserv/Changes +++ b/gameserv/Changes @@ -1,4 +1,6 @@ Version 1.3.4 +* Fixed a bunch of memory leaks including one when a monster is found in the forest, when + the dragon is fought, and when a master is fought - Kain * Fixed a bug that would leave a player logged in after they quit - Kain (thanks Mauritz) Version 1.3.3 * Fixed a bug causing players' HP to be set to 10 on gameserv load - Kain (thanks Mauritz) diff --git a/gameserv/aClient.cpp b/gameserv/aClient.cpp index 36ebd11..8de6b35 100644 --- a/gameserv/aClient.cpp +++ b/gameserv/aClient.cpp @@ -92,22 +92,22 @@ ostream &operator<<( ostream &out, const aClient &c ) void aClient::setData(const aClient *right) { - if (right != this) + if (right != this) { - strcpy(nick, right->nick); - #ifdef P10 - strcpy(realnick, right->realnick); - #endif - if (right->stats) - { - if (!stats) - stats = new Player; - #ifdef DEBUGMODE - log("Should be setting data for %s", right->stats->getName().c_str()); - #endif - - stats->setData(right->stats); - } + strcpy(nick, right->nick); +#ifdef P10 + strcpy(realnick, right->realnick); +#endif + if (right->stats) + { + if (!stats) + stats = new Player; +#ifdef DEBUGMODE + log("Should be setting data for %s", right->stats->getName().c_str()); +#endif + + stats->setData(right->stats); + } } } diff --git a/gameserv/c_forest.cpp b/gameserv/c_forest.cpp index 401c955..c20cfb4 100644 --- a/gameserv/c_forest.cpp +++ b/gameserv/c_forest.cpp @@ -73,10 +73,8 @@ void do_forest(char *u) // 90% of forest searching turns up a monster if (eventnum >= 10) { - Monster *tempmonster; - tempmonster = new Monster(levels[p->getLevel() - 1].randomMonster()); - p->setMonster(tempmonster); - delete tempmonster; + p->setMonster(levels[p->getLevel() - 1].randomMonster()); + notice(s_GameServ, u, "You have found %s!", p->getMonster()->name.c_str()); if (p->getMonster()->hp < p->getMonster()->maxhp) p->getMonster()->hp = p->getMonster()->maxhp; diff --git a/gameserv/gameserv.cpp b/gameserv/gameserv.cpp index aba195e..63100a3 100644 --- a/gameserv/gameserv.cpp +++ b/gameserv/gameserv.cpp @@ -3180,7 +3180,7 @@ void do_dragon(char *u) notice(s_GameServ, u, "Just then you notice the eye begin to "\ "glare orange! The tooth is moving... but it is still too "\ "dark for you to make out.... THE DRAGON! You see it!"); - p->setMonster(new Monster(dragon)); + p->setMonster(&dragon); setDragonFight(p); display_monster(u); } @@ -3325,11 +3325,9 @@ void see_master(char *u) if (!is_fighting(user) && is_playing(user)) { - Monster *temp; Player *p = user->stats; - temp = new Monster(levels[p->getLevel() - 1].master); - p->setMyMaster(temp); - p->setMonster(temp); + p->setMyMaster(&levels[p->getLevel() - 1].master); + p->setMonster(&levels[p->getLevel() - 1].master); display_monster(u); // Since master is the same structure, use this function } } @@ -3780,7 +3778,10 @@ bool load_levels() { sprintf(filename, "data/levels/level%d.dat", x); if (levels[x - 1].loadLevel(filename) == false) - return false; + { + delete []filename; + return false; + } } delete []filename; @@ -3803,6 +3804,8 @@ bool load_monsters() if (!infile) { log("Error opening %s", filename); + delete []filename; + delete []buf; return false; } diff --git a/gameserv/level.cpp b/gameserv/level.cpp index 938c990..b84d431 100644 --- a/gameserv/level.cpp +++ b/gameserv/level.cpp @@ -10,55 +10,55 @@ using std::ifstream; long int range::random() { - if (high == 0 || high <= low) + if (high == 0 || high <= low) return 0; - else + else return low + rand() % (high - low); } void range::setRange(char *r) { - low = stringtoint(strtok(r, "~")); - high = stringtoint(strtok(NULL, "")); + low = stringtoint(strtok(r, "~")); + high = stringtoint(strtok(NULL, "")); } Level::Level() { - strength.high = 0; - strength.low = 0; - - gold.high = 0; - gold.low = 0; - - exp.high = 0; - exp.low = 0; - - hp.high = 0; - hp.low = 0; + strength.high = 0; + strength.low = 0; + + gold.high = 0; + gold.low = 0; + + exp.high = 0; + exp.low = 0; + + hp.high = 0; + hp.low = 0; } void Level::setStrength(range &s) { - strength.high = s.high; - strength.low = s.low; + strength.high = s.high; + strength.low = s.low; } void Level::setGold(range &g) { - gold.high = g.high; - gold.low = g.low; + gold.high = g.high; + gold.low = g.low; } void Level::setExp(range &e) { - exp.high = e.high; - exp.low = e.low; + exp.high = e.high; + exp.low = e.low; } void Level::setHP(range &h) { - hp.high = h.high; - hp.low = h.low; + hp.high = h.high; + hp.low = h.low; } Monster *Level::randomMonster() @@ -66,51 +66,51 @@ Monster *Level::randomMonster() int num, x; Monster *ptr; list::iterator iter; - + num = rand() % monsters.size(); for (x = 0, iter = monsters.begin(); x < num; x++) { iter++; } - + ptr = (*iter); ptr->strength = strength.random(); ptr->gold = gold.random(); ptr->exp = exp.random(); ptr->hp = hp.random(); ptr->maxhp = ptr->hp; - return ptr; + return ptr; } bool Level::loadLevel(char *filename) { - ifstream infile(filename); - char *buf; - - #ifdef DEBUGMODE - log("Attempting to open %s", filename); - #endif - - if (infile.fail()) + ifstream infile(filename); + char *buf; + +#ifdef DEBUGMODE + log("Attempting to open %s", filename); +#endif + + if (infile.fail()) return false; - - buf = new char[32]; - - for (int x = 0;infile.getline(buf, 32, '\n'); x++) + + buf = new char[32]; + + for (int x = 0;infile.getline(buf, 32, '\n'); x++) { - if (buf[0] == '^') + if (buf[0] == '^') break; - else if (x == 0) - { - strength.setRange(buf); - } - else if (x == 1) + else if (x == 0) + { + strength.setRange(buf); + } + else if (x == 1) gold.setRange(buf); - else if (x == 2) + else if (x == 2) exp.setRange(buf); - else if (x == 3) + else if (x == 3) hp.setRange(buf); } - delete []buf; - return true; + delete []buf; + return true; } diff --git a/gameserv/log.cpp b/gameserv/log.cpp index 8bc55a3..195dc55 100644 --- a/gameserv/log.cpp +++ b/gameserv/log.cpp @@ -10,61 +10,63 @@ using std::ios; void log(const char *fmt, ...) { - if (fmt[0] == '\0') - return; - - ofstream outfile; - char *ts, *output; - const char *t = fmt; - - ts = new char[64]; - output = new char[4096]; - - outfile.open("gameserv.log", ios::out | ios::app); - - if (outfile.fail()) - { - cerr << "Error opening gameserv.log" << endl; + if (fmt[0] == '\0') return; - } - - struct tm *tm; - time_t ti; - time(&ti); - tm = localtime(&ti); - strftime(ts, 64, "%m/%d/%Y %H:%M:%S", tm); - - sprintf(output, "[%s]: ", ts); - - va_list args; - va_start(args, fmt); - - for (; *t; t++) + + ofstream outfile; + char *ts, *output; + const char *t = fmt; + + ts = new char[64]; + output = new char[4096]; + + outfile.open("gameserv.log", ios::out | ios::app); + + if (outfile.fail()) + { + delete []ts; + delete []output; + cerr << "Error opening gameserv.log" << endl; + return; + } + + struct tm *tm; + time_t ti; + time(&ti); + tm = localtime(&ti); + strftime(ts, 64, "%m/%d/%Y %H:%M:%S", tm); + + sprintf(output, "[%s]: ", ts); + + va_list args; + va_start(args, fmt); + + for (; *t; t++) { - if (*t == '%') + if (*t == '%') { - switch(*++t) { - case 'd': sprintf(output, "%s%d", output, va_arg(args, int)); break; - case 's': sprintf(output, "%s%s", output, va_arg(args, char *)); break; - case 'S': sprintf(output, "%s%s", output, s_GameServ); break; - case 'c': sprintf(output, "%s%c", output, va_arg(args, int)); break; - case 'l': - if (*++t == 'd') - sprintf(output, "%s%ld", output, va_arg(args, long int)); break; - } + switch(*++t) { + case 'd': sprintf(output, "%s%d", output, va_arg(args, int)); break; + case 's': sprintf(output, "%s%s", output, va_arg(args, char *)); break; + case 'S': sprintf(output, "%s%s", output, s_GameServ); break; + case 'c': sprintf(output, "%s%c", output, va_arg(args, int)); break; + case 'l': + if (*++t == 'd') + sprintf(output, "%s%ld", output, va_arg(args, long int)); break; + } } - else + else { - sprintf(output, "%s%c", output, *t); + sprintf(output, "%s%c", output, *t); } - + } - - outfile << output << endl; - - outfile.close(); - va_end(args); - - delete [] ts; - delete [] output; + + outfile << output << endl; + + outfile.close(); + va_end(args); + + delete [] ts; + delete [] output; } diff --git a/gameserv/news.cpp b/gameserv/news.cpp index 14933a4..9b7c988 100644 --- a/gameserv/news.cpp +++ b/gameserv/news.cpp @@ -19,49 +19,49 @@ void loadNews(char *filename, list &news); void addNews(list &news, const char *fmt, ...) { - if (fmt[0] == '\0') - return; - - va_list args; - char *input; - input = new char[1024]; - memset(input, 0, 1024); - const char *t = fmt; - - va_start(args, fmt); - - for (; *t; t++) + if (fmt[0] == '\0') + return; + + va_list args; + char *input; + input = new char[1024]; + memset(input, 0, 1024); + const char *t = fmt; + + va_start(args, fmt); + + for (; *t; t++) { - if (*t == '%') + if (*t == '%') { - switch(*++t) { - case 'd': sprintf(input, "%s%d", input, va_arg(args, int)); break; - case 's': sprintf(input, "%s%s", input, va_arg(args, char *)); break; - case 'S': sprintf(input, "%s%s", input, s_GameServ); break; - case 'l': - if (*++t == 'd') - sprintf(input, "%s%ld", input, va_arg(args, long int)); break; - } + switch(*++t) { + case 'd': sprintf(input, "%s%d", input, va_arg(args, int)); break; + case 's': sprintf(input, "%s%s", input, va_arg(args, char *)); break; + case 'S': sprintf(input, "%s%s", input, s_GameServ); break; + case 'l': + if (*++t == 'd') + sprintf(input, "%s%ld", input, va_arg(args, long int)); break; + } } - else + else { - sprintf(input, "%s%c", input, *t); + sprintf(input, "%s%c", input, *t); } - + } - #ifdef DEBUGMODE - log("New News Item: %s", input); - #endif - - notice(s_GameServ, c_Forest, "News Flash: %s", input); - string *nstring; - nstring = new string(input); - news.push_back(*nstring); - - delete [] input; - delete nstring; - -va_end(args); +#ifdef DEBUGMODE + log("New News Item: %s", input); +#endif + + notice(s_GameServ, c_Forest, "News Flash: %s", input); + string *nstring; + nstring = new string(input); + news.push_back(*nstring); + + delete [] input; + delete nstring; + + va_end(args); } void showNews(char *nick, list &news) @@ -100,38 +100,38 @@ void saveNews(char *filename, list &news) void loadNews(char *filename, list &news) { - // First clear the old news out - news.clear(); - - // Now load from the file - ifstream infile; - infile.open(filename); - if (infile.fail()) - { - log("Error opening %s", filename); - return; - } - - char *buf; - string *str; - buf = new char [1024]; - - while (infile.getline(buf, 1024, '\n')) - { - if (buf[0] == '\0' || buf[0] == '\n') - { - delete [] buf; - return; - } - str = new string(buf); - news.push_back(*str); - delete str; - } - - delete [] buf; + // First clear the old news out + news.clear(); + + // Now load from the file + ifstream infile; + infile.open(filename); + if (infile.fail()) + { + log("Error opening %s", filename); + return; + } + + char *buf; + string *str; + buf = new char [1024]; + + while (infile.getline(buf, 1024, '\n')) + { + if (buf[0] == '\0' || buf[0] == '\n') + { + delete [] buf; + return; + } + str = new string(buf); + news.push_back(*str); + delete str; + } + + delete [] buf; } void do_news(char *u) { - showNews(u, todaysnews); + showNews(u, todaysnews); } diff --git a/gameserv/player.cpp b/gameserv/player.cpp index dbdaa1d..eec8743 100644 --- a/gameserv/player.cpp +++ b/gameserv/player.cpp @@ -46,6 +46,8 @@ void Player::reset() Player::Player() { + if (inventory != NULL) + delete inventory; inventory = new pouch(); setData(NULL); @@ -56,6 +58,8 @@ Player::Player() Player::Player(char *n) { + if (inventory != NULL) + delete inventory; inventory = new pouch(); reset(); // Set defaults @@ -68,6 +72,8 @@ Player::Player(char *n) Player::Player(string n) { + if (inventory != NULL) + delete inventory; inventory = new pouch(); reset(); name = n; @@ -79,22 +85,17 @@ Player::Player(string n) Player::~Player() { delete inventory; - + #ifdef DEBUGMODE string *output; output = new string("Deleting Player"); -#endif - -#ifdef DEBUGMODE *output += ": " + name; -#endif - -#ifdef DEBUGMODE *output += " Password: " + password; log ("%s", output->c_str()); delete output; #endif } + void Player::setPassword(const char *p) { static char saltChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV\ diff --git a/gameserv/tcpclient.cpp b/gameserv/tcpclient.cpp index f3c7aba..48e7823 100644 --- a/gameserv/tcpclient.cpp +++ b/gameserv/tcpclient.cpp @@ -49,6 +49,7 @@ void prettyIntro(); void check_idles(); void clearClients(); void clearPlayers(); +void clearItems(); // Make this a daemon int daemon(int nochdir, int noclose); @@ -80,6 +81,7 @@ int main(int argc, char *argv[]) cout << "Usage: gameserv [options] [configfile]" << endl; cout << "Options:" << endl; cout << "--help Displays this help dialogue" << endl; + delete []conf; return 1; } delete []conf; @@ -518,34 +520,38 @@ int main(int argc, char *argv[]) source--; #if defined(P10) - } else if (stricmp(cmd, "P") == 0) { - char *rest, *dest; - char *longname; - longname = new char[strlen(s_GameServ) + strlen(servername) + 2]; - - sprintf(longname, "%S@%s", servername); - - dest = strtok(NULL, " "); - rest = strtok(NULL, ""); - if (stricmp(dest, gsnum) == 0 || stricmp(dest, longname) == 0) - { - delete [] longname; - gameserv(source, rest); - } - else if (stricmp(dest, c_Forest) == 0 && isListenOnCF()) - { - delete [] longname; - forest(source, rest); - } + } + else if (stricmp(cmd, "P") == 0) + { + char *rest, *dest; + char *longname; + longname = new char[strlen(s_GameServ) + strlen(servername) + 2]; + + sprintf(longname, "%S@%s", servername); + + dest = strtok(NULL, " "); + rest = strtok(NULL, ""); + if (stricmp(dest, gsnum) == 0 || stricmp(dest, longname) == 0) + { + delete [] longname; + gameserv(source, rest); + } + else if (stricmp(dest, c_Forest) == 0 && isListenOnCF()) + { + delete [] longname; + forest(source, rest); + } #else - } else if (stricmp(cmd, "PRIVMSG") == 0) { + } + else if (stricmp(cmd, "PRIVMSG") == 0) + { char *rest, *dest; dest = strtok(NULL, " "); rest = strtok(NULL, ""); if (strnicmp(dest, s_GameServ, strlen(s_GameServ)) == 0) - gameserv(source, rest); + gameserv(source, rest); else if (stricmp(dest, c_Forest) == 0 && isListenOnCF()) - forest(source, rest); + forest(source, rest); #endif #if defined(P10) } else if (stricmp(cmd, "J") == 0) { @@ -622,7 +628,7 @@ int main(int argc, char *argv[]) saveNews(newsdata, todaysnews); clearClients(); clearPlayers(); - + clearItems(); delete_monsters(); #ifdef DEBUGMODE @@ -947,7 +953,15 @@ void save_lastrollover() outfile << lastrollover << endl; outfile.close(); } - +void clearItems() +{ + list::iterator iter; + for (iter = Items.begin(); iter != Items.end(); iter++) + { + delete (*iter); + Items.erase(iter); + } +} void clearClients() { list::iterator iter; @@ -960,6 +974,7 @@ void clearClients() } } } + void clearPlayers() { list::iterator iter;