Skip to content

Commit 7494f47

Browse files
committed
clear ship registry after mission
Create a `ship_level_close()` function and call it from `game_level_close()`. Among other tasks, this clears the ship registry to prevent SEXPs or scripts from obtaining references to ships that have been deleted. Follow-up to #7522.
1 parent 741693a commit 7494f47

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

code/ship/ship.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6914,6 +6914,18 @@ void ship_level_init()
69146914
Man_thruster_reset_timestamp = timestamp(0);
69156915
}
69166916

6917+
void ship_level_close()
6918+
{
6919+
// at this point ships have all gone through ship_delete,
6920+
// so don't leave any stray references behind (e.g. for sexps)
6921+
Ship_registry.clear();
6922+
Ship_registry_map.clear();
6923+
6924+
ship_close_cockpit_displays(Player_ship);
6925+
6926+
Num_wings = 0;
6927+
}
6928+
69176929
/**
69186930
* Add a ship onto the exited ships list.
69196931
*
@@ -8745,6 +8757,7 @@ void ship_close_cockpit_displays(ship* shipp)
87458757
{
87468758
if (shipp && shipp->cockpit_model_instance >= 0) {
87478759
model_delete_instance(shipp->cockpit_model_instance);
8760+
shipp->cockpit_model_instance = -1;
87488761
}
87498762

87508763
for ( int i = 0; i < (int)Player_displays.size(); i++ ) {

code/ship/ship.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,7 @@ extern SCP_vector<wing_formation> Wing_formations;
16881688

16891689
extern void ship_init(); // called once at game start
16901690
extern void ship_level_init(); // called before the start of each level
1691+
extern void ship_level_close(); // called after the end of each level
16911692

16921693
//returns -1 if failed
16931694
extern int ship_create(matrix* orient, vec3d* pos, int ship_type, const char* ship_name = nullptr, bool standalone_ship = false);

freespace2/freespace.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ void game_level_close()
984984
particle::ParticleManager::get()->clearSources();
985985
particle::close();
986986
trail_level_close();
987-
ship_close_cockpit_displays(Player_ship);
987+
ship_level_close();
988988
hud_level_close();
989989
hud_escort_clear_all();
990990
model_instance_free_all();

0 commit comments

Comments
 (0)