for (vch = ch->in_room->people; vch != NULL; vch = vch_next)
vch_next = vch->next_in_room;
for(vch = ch->in_room->people; vch != NULL; vch = vch_next){
vch_next = vch->next_in_room;
//…rest if loop here (those 2 ifs)
}
Program received signal SIGSEGV, Segmentation fault.
room_is_dark (pRoomIndex=0x0) at handler.c:2539
2539 if (pRoomIndex->light > 0)
(gdb) tb
Breakpoint 1 at 0x4433e3: file handler.c, line 2539.
(gdb) bt
#0 room_is_dark (pRoomIndex=0x0) at handler.c:2539
#1 0x00443628 in can_see (ch=0x12975fc, victim=0x13297f0) at handler.c:2640
#2 0x00437672 in violence_update () at fight.c:107
#3 0x00479f33 in update_handler () at update.c:1199
#4 0x0042c442 in game_loop_unix (control=5) at comm.c:844
#5 0x0042bed8 in main (argc=2, argv=0x11419b0) at comm.c:448
(gdb)
Program received signal SIGSEGV, Segmentation fault.
room_is_dark (pRoomIndex=0x0) at handler.c:2539
2539 if (pRoomIndex->light > 0)
(gdb) tb
Breakpoint 1 at 0x4433e3: file handler.c, line 2539.
(gdb) bt
#0 room_is_dark (pRoomIndex=0x0) at handler.c:2539
#1 0x00443628 in can_see (ch=0x12975fc, victim=0x13297f0) at handler.c:2640
#2 0x00437672 in violence_update () at fight.c:107
#3 0x00479f33 in update_handler () at update.c:1199
#4 0x0042c442 in game_loop_unix (control=5) at comm.c:844
#5 0x0042bed8 in main (argc=2, argv=0x11419b0) at comm.c:448
(gdb)
void violence_update (void)
{
CHAR_DATA *ch;
CHAR_DATA *ch_next;
CHAR_DATA *victim;
CHAR_DATA *vch_next;
for (ch = char_list; ch != NULL; ch = ch_next)
{
ch_next = ch->next;
if ((victim = ch->fighting) == NULL || ch->in_room == NULL)
continue;
if (IS_AWAKE (ch) && ch->in_room == victim->in_room)
{
multi_hit (ch, victim, TYPE_UNDEFINED);
/*
* Swords will very rarely get in an extra round.
*/
if (get_weapon_sn (ch) == gsn_sword
&& ch->wait <= 0)
{
int chance;
CHAR_DATA * was_fighting;
CHAR_DATA * vch;
chance = get_skill (ch, gsn_sword) / 15;
/*
* Penalty if the opponent can see.
*/
if (can_see (victim, ch))
chance = chance * 3 / 5;
for (vch = ch->in_room->people; vch != NULL; vch = vch_next){
vch_next = vch->next_in_room;
if (vch->fighting == ch
|| is_same_group (vch->fighting, ch))
{
if (number_percent () < chance)
{
was_fighting = ch->fighting;
ch->fighting = vch;
multi_hit (ch, vch, TYPE_UNDEFINED);
check_improve (ch, gsn_sword, TRUE, 2);
ch->fighting = was_fighting;
WAIT_STATE (ch, PULSE_VIOLENCE);
break;
}
}
}
}
}
else
stop_fighting (ch, FALSE);
if ((victim = ch->fighting) == NULL)
continue;
/*
* Fun for the whole family!
*/
check_assist (ch, victim);
if (IS_NPC (ch))
{
if (HAS_TRIGGER (ch, TRIG_FIGHT))
mp_percent_trigger (ch, victim, NULL, NULL, TRIG_FIGHT);
if (HAS_TRIGGER (ch, TRIG_HPCNT))
mp_hprct_trigger (ch, victim);
}
}
return;
}
In fact, what's going on here is a good example of why you want to use braces immediately after the loop/if/whatever. :smile:
and in effect, vch (or at least the loop iteration) goes out of scope, it does not matter. The point is that
as the loop is not continued after the loop pointer might have gone invalid, a vch_next holder is not needed.
It might be argued that a new coder might not be able to determine when a holder is needed and should
use one all the time. But this was brought up as a side issue (that had nothing to do with his original
problem) and was just one more thing that has muddled the whole issue.
I thought that the original problem here was that the victim might have died and possibly been extracted as a consequence. It sounded to me earlier like you were saying that the victim dying wasn't an issue, but I think based on what you just said that it's not an issue only as far as the loop counter is concerned. Is that what you meant?