Login
User Name:

Password:



Register
Forgot your password?
Vote for Us!
 Couple bugs
Yesterday, 5:42 pm
By Remcon
Bug in disarm( )
Nov 12, 2017, 6:54 pm
By GatewaySysop
Bug in will_fall( )
Oct 23, 2017, 1:35 am
By GatewaySysop
Bug in do_zap( ), do_brandish( )
Oct 18, 2017, 1:52 pm
By GatewaySysop
Bug in get_exp_worth( )
Oct 10, 2017, 1:26 am
By GatewaySysop
LOP 1.45
Author: Remcon
Submitted by: Remcon
LOP Heroes Edition
Author: Vladaar
Submitted by: Vladaar
Heroes sound extras
Author: Vladaar
Submitted by: Vladaar
6Dragons 4.3
Author: Vladaar
Submitted by: Vladaar
Memwatch
Author: Johan Lindh
Submitted by: Vladaar
Users Online
CommonCrawl, Yahoo!, DotBot

Members: 0
Guests: 15
Stats
Files
Topics
Posts
Members
Newest Member
477
3,705
19,232
608
LAntorcha
Today's Birthdays
There are no member birthdays today.
Related Links
» SmaugMuds.org » Codebases » AFKMud Support & Development » free_channels() doing somethi...
Forum Rules | Mark all | Recent Posts

free_channels() doing something naughty.
< Newer Topic :: Older Topic >

Pages:<< prev 1 next >>
Post is unread #1 Apr 13, 2010, 6:53 pm   Last edited Apr 13, 2010, 6:54 pm by Kayle
Go to the top of the page
Go to the bottom of the page

Kayle
Off the Edge of the Map
GroupAdministrators
Posts1,195
JoinedMar 21, 2006

Currently, AFKMud's free_channels (and possibly other functions similar to it) are doing something naughty by deleting a list member while using an iterator. This can lead to the iterator becoming corrupted, and the whole thing just fails to do what it was intended to do. The easiest way to fix it is to do something like this:

void free_channels( void )
{
   Channel *channel;

   while( chanlist.empty(  ) == false ) 
   { 
      channel = *( chanlist.begin(  ) ); 
      chanlist.remove( channel ); 
      delete channel; 
   }
   return;
}


This prevents the iterator from becoming corrupted, because there isn't one, and still removes every list member from the game and memory, as was intended.

[Edit:] Btw, that code is pulled from SW:TSW which uses AFKMud's channel code, so the define for channel may be different if you're using AFKMud. >.>
       
Post is unread #2 Apr 15, 2010, 8:29 am   Last edited Apr 15, 2010, 8:30 am by David Haley
Go to the top of the page
Go to the bottom of the page

David Haley
Sorcerer
GroupMembers
Posts903
JoinedJan 29, 2007

Another option:

void free_channels(void)
{
  std::list<Channel*>::iterator it;
  for (it = chanlist.begin(); it != chanlist.end(); it++) {
    delete *it;
  }
  chanlist.clear();
}


Or if you want to get down with the funky groove,
void channel_deleter(Channel* c) {
  delete c;
}

void free_channels(void)
{
  for_each(chanlist.begin(), chanlist.end(), channel_deleter);
  chanlist.clear();
}


I really like the second option in principle, but C++ makes it annoying to use.



See also documentation on for_each.
       
Post is unread #3 Apr 15, 2010, 11:39 am   Last edited Apr 15, 2010, 11:41 am by Caius
Go to the top of the page
Go to the bottom of the page

Caius
Magician
GroupMembers
Posts132
JoinedJan 29, 2006

Here's another cool solution. Like David's second option, but without the need for a deleter function for every type. This is from Scott Meyers' "Effective STL".

// Put this class in some header for reusability
struct DeleteObject
{
  template< typename T >
  void operator()( const T *ptr ) const
  {
    delete ptr;
  }
};

// Client code
void free_channels()
{
  for_each(chanlist.begin(), chanlist.end(), DeleteObject());
  chanlist.clear();
}


       
Pages:<< prev 1 next >>