11 Jul, 2009, Banner wrote in the 1st comment:
Votes: 0
void update_space( void )
{
std::list< SHIP_DATA *>::iterator ship;

log_string( "Running update_space" );
for( ship = list_ships_gnd.begin(); ship != list_ships_gnd.end(); ship++ )
{
if( (*ship)->shipstate == SHIP_LAUNCH )
launchship((*ship));
}
}


gdb:
[Fri Jul 10 23:37:47 2009 :: Preloading player data for: prototype (1K)
Fri Jul 10 23:37:47 2009 :: Loading player data for: Prototype (1K)
Fri Jul 10 23:37:48 2009 :: Running update_space
Fri Jul 10 23:37:58 2009 :: Running update_space
Fri Jul 10 23:37:58 2009 :: Running launchship

Program received signal SIGSEGV, Segmentation fault.
0x08201593 in update_space () at space2.c:1079
1079 if( (*ship)->shipstate == SHIP_LAUNCH )
Current language: auto; currently c++
(gdb) print (*ship)->name
Cannot access memory at address 0x8
(gdb) print *(*ship)
Cannot access memory at address 0x8
(gdb) print (*ship)
$1 = (SHIP_DATA *&) @0x8: <error reading variable>
(gdb)
[/code]

The list has items in it and I can access it elsewhere, but the curiousness here is that this runs update_space fine until I try to launch my ship, which sets ship->shipstate to SHIP_LAUNCH, at which point the next time space updates, it crashes with this error. Any suggestions?
11 Jul, 2009, Guest wrote in the 2nd comment:
Votes: 0
Does your launchship() function take things off of your list of ships on the ground?
11 Jul, 2009, Banner wrote in the 3rd comment:
Votes: 0
Launchship calls extract_ship, which takes the ship from ships_on_ground and then calls ship_to_space which puts the ship in a list of ships_in_space.

EDIT: I take it that screws with the iteration so I need to remove it from the ground list after the functions complete or something?
11 Jul, 2009, Guest wrote in the 4th comment:
Votes: 0
Well you could try this and see if it works:

void update_space( void )
{
std::list< SHIP_DATA *>::iterator iship;

log_string( "Running update_space" );
for( iship = list_ships_gnd.begin(); iship != list_ships_gnd.end(); )
{
SHIP_DATA *ship = *iship;
++iship;

if( ship->shipstate == SHIP_LAUNCH )
launchship(ship);
}
}


It's messy and ugly, but I've got quite a few loops that work this way. I'm far from the best person to take advice on proper handling of a std::list though.
11 Jul, 2009, Runter wrote in the 5th comment:
Votes: 0
Keep in mind you can do something like:

it = list.erase(it);


list::erase will return the correct value for the next iteration.

Not sure how this would help you here but maybe in the future it could. :)
11 Jul, 2009, Banner wrote in the 6th comment:
Votes: 0
Samson said:
Well you could try this and see if it works:

void update_space( void )
{
std::list< SHIP_DATA *>::iterator iship;

log_string( "Running update_space" );
for( iship = list_ships_gnd.begin(); iship != list_ships_gnd.end(); )
{
SHIP_DATA *ship = *iship;
++iship;

if( ship->shipstate == SHIP_LAUNCH )
launchship(ship);
}
}


It's messy and ugly, but I've got quite a few loops that work this way. I'm far from the best person to take advice on proper handling of a std::list though.


Awesome, awesome. Thanks Samson. Now can you tell me why that does that and when I need to loop like that?
11 Jul, 2009, Runter wrote in the 7th comment:
Votes: 0
If you need to delete the value you are iterating you need to know what the next thing to iterate is. So he did the ++ before the code that removed it fromt he list. Because after the fact, it wouldn't have been valid.

The code I posted above will let you delete the current iterator from the list since it returns the correct location of the next item in the list.
12 Jul, 2009, Banner wrote in the 8th comment:
Votes: 0
for( bShip = list_ships_hyp.begin(); bShip != list_ships_hyp.end(); )
{
SHIP_DATA *ship2 = *bShip;
++bShip;

if( ship2->shipstate == SHIP_HYPERSPACE )
{
log_string( "Updating a hypership" );
ship2->hyperdistance -= ship2->hyperspeed * 2;
if( ship2->hyperdistance <= 0 )
{
ship_to_space( ship2 );

if( ship2->space == NULL )
{
echo_to_cockpit( AT_RED, ship2, "Ship lost in Hyperspace. Make new calculations." );
}
else
{
echo_to_room( AT_YELLOW, get_room_index( ship2->pilotseat ), "Hyperjump complete." );
echo_to_ship( AT_YELLOW, ship2, "The ship lurches slightly as it comes out of hyperspace." );
sprintf( buf, "%s enters the starsystem at %.0lf %.0lf %.0lf",
ship2->name, ship2->course->x, ship2->course->y, ship2->course->z );
echo_to_space( AT_YELLOW, ship2, buf, NULL );
ship2->shipstate = SHIP_READY;
if( ship2->space->name)
{
STRFREE(ship2->home );
ship2->home = STRALLOC(ship2->space->name );
}
if( str_cmp( "Public", ship2->owner ) )
save_ship( ship2 );
}
}
else
{
sprintf( buf, "&gRemaining Jump Distance: &w%d", ship2->hyperdistance );
echo_to_room( AT_WHITE, get_room_index( ship2->pilotseat ), buf );
}
}
}
}




[Health] 32700/32700 [Faith] 104/104 [Energy] 110/110]|> You can only do that in realspace!
Log: Updating space
Log: Updating a hypership
Remaining Jump Distance: 7368
Log: Updating a hypership
Remaining Jump Distance: 6858
Log: Updating a hypership
Remaining Jump Distance: 6348
Log: Updating a hypership
Remaining Jump Distance: 5838

[Health] 32700/32700 [Faith] 104/104 [Energy] 110/110]|>
Log: Updating space
Log: Updating a hypership
Remaining Jump Distance: 5328
Log: Updating a hypership
Remaining Jump Distance: 4818
Log: Updating a hypership
Remaining Jump Distance: 4308
Log: Updating a hypership
Remaining Jump Distance: 3798


For whatever reason, it's spamming the message multiple times and I'mnot sure why. :-/
12 Jul, 2009, Banner wrote in the 9th comment:
Votes: 0
Shoved it in the list too many times, woops.
0.0/9