Re: [CODE] Arena affections crash

From: Chris Gilbert (chris@buzzbee.freeserve.co.uk)
Date: 09/01/00


Melissa Jadwinski wrote:

I think that you've what's happening is that you're doing your next bit
wrong, easily done though :)
The problem is that affect_remove will free up the aff structure, come
the next loop iteration you're looking at junk in aff->next, what you
need is something like (lines I've added/changed have a @ in front:

[snip]
> void arena_store_affects(struct char_data *ch, struct combatant_info_type
> *comb) {
@   struct affected_type *aff, *new_aff, *next_aff;
>
>   comb->affected = NULL;
>
@   for (aff = ch->affected; aff; aff = next_aff) {
@     /* preserve pointer to next affect */
@     next_aff=aff->next;
>     CREATE(new_aff, struct affected_type, 1);
>     clear_affect(new_aff);
>
>     new_aff->type       = aff->type;  /* <-- Crashes here */
>     new_aff->idnum      = aff->idnum;
>     new_aff->duration   = aff->duration;
>     new_aff->modifier   = aff->modifier;
>     new_aff->location   = aff->location;
>     new_aff->bitvector  = aff->bitvector;
>     new_aff->bitvector1 = aff->bitvector1;
>
>     new_aff->next = comb->affected;
>     comb->affected = new_aff;

@/* this frees up aff, that's we stored the pointer to the next affect
earlier */
>     affect_remove(ch, aff);
>
>   }
>
> }
[snip]

You'll probably find this style of for loop quite common for places
where memory is being freed inside the loop.

Thinking on it you're always removing the first affect, so you could
make the aff = ch->affected as the next value of aff, but that would
probably look really odd, and cause no end of trouble for future coders
that look at it and go 'eh?'

Cheers,
Chris


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     |  http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html  |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 04/11/01 PDT