]> jfr.im git - irc/gameservirc.git/commitdiff
fixed a ton of memory leaks
authorkainazzzo <redacted>
Sun, 2 Apr 2006 03:34:01 +0000 (03:34 +0000)
committerkainazzzo <redacted>
Sun, 2 Apr 2006 03:34:01 +0000 (03:34 +0000)
git-svn-id: https://svn.code.sf.net/p/gameservirc/code/trunk@439 bc333340-6410-0410-a689-9d09f3c113fa

gameserv/Changes
gameserv/aClient.cpp
gameserv/c_forest.cpp
gameserv/gameserv.cpp
gameserv/level.cpp
gameserv/log.cpp
gameserv/news.cpp
gameserv/player.cpp
gameserv/tcpclient.cpp

index 133d62913ed6155e5b914a570e78208bb88aac0c..37a7e8a6160a488b906e7f3279ef56ac103e6393 100644 (file)
@@ -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)
index 36ebd11063ca21f80e7c5ab57f3782d935512dab..8de6b3567170b0c262d149a3f562b91e4f7c938e 100644 (file)
@@ -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);
+               }
     }
 }
 
index 401c9557ca7638a8cfe1b438a7829992c1465ab2..c20cfb43fa2aa33372b1ae82cde147457e3cf5dd 100644 (file)
@@ -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 \ 2%s\ 2!", p->getMonster()->name.c_str());
                          if (p->getMonster()->hp < p->getMonster()->maxhp)
                                p->getMonster()->hp = p->getMonster()->maxhp;
index aba195ee0115096aeff371e4d24af003ee69b084..63100a3be73cb686b4a27dd294dd87a483ab5791 100644 (file)
@@ -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;
                }
          
index 938c990e6245171a59707f79b2dc97e8e420787a..b84d4315703edf970438ffeff160b6b1ab588eff 100644 (file)
@@ -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<Monster*>::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;
 }
index 8bc55a3eda52c0ffb5b4077baddb30e73cabc840..195dc55b51b13a95aaed0ed8a335e91c81050a74 100644 (file)
@@ -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;
 }
index 14933a40225d6e1337595bbd339102e829e16838..9b7c988501872cd899a331ca3bf8928123f8ea9f 100644 (file)
@@ -19,49 +19,49 @@ void loadNews(char *filename, list<string> &news);
 
 void addNews(list<string> &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<string> &news)
@@ -100,38 +100,38 @@ void saveNews(char *filename, list<string> &news)
 
 void loadNews(char *filename, list<string> &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);
 }
index dbdaa1dffc4f1b25c53957c398f4e2fb60bbd2e4..eec8743503a78ae0c52070c8cfb797e5cad6edbc 100644 (file)
@@ -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\
index f3c7aba0feeb68b50573ef75e51391db49603fa4..48e7823371ba1ec40f001774ab3b11b775d6c9b2 100644 (file)
@@ -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<item*>::iterator iter;
+  for (iter = Items.begin(); iter != Items.end(); iter++)
+       {
+         delete (*iter);
+         Items.erase(iter);
+       }
+}
 void clearClients()
 {
   list<aClient*>::iterator iter;
@@ -960,6 +974,7 @@ void clearClients()
                }
        }
 }
+
 void clearPlayers()
 {
   list<Player*>::iterator iter;