Skip to content

Commit

Permalink
Fix resizing advisor data when switching ruleset
Browse files Browse the repository at this point in the history
Move to std::vector. This fixes a crash when switching from the default ruleset
to civ2civ3 or Royale, which have more governments.

Thanks to blabber for reporting.
  • Loading branch information
lmoureaux committed Jan 4, 2024
1 parent 23bf528 commit ce2d762
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
11 changes: 4 additions & 7 deletions server/advisors/advdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ struct adv_data *adv_data_get(struct player *pplayer, bool *caller_closes)
}

/**
Allocate memory for advisor data. Save to call multiple times.
Allocate memory for advisor data. Safe to call multiple times.
*/
void adv_data_init(struct player *pplayer)
{
Expand All @@ -691,7 +691,7 @@ void adv_data_init(struct player *pplayer)
}
adv = pplayer->server.adv;

adv->government_want = nullptr;
adv->government_want.clear();

adv->dipl.adv_dipl_slots = new adv_dipl *[MAX_NUM_PLAYER_SLOTS]();
player_slots_iterate(pslot)
Expand Down Expand Up @@ -724,9 +724,7 @@ void adv_data_default(struct player *pplayer)
fc_assert_ret(adv != nullptr);

adv->govt_reeval = 0;
if (!adv->government_want) {
adv->government_want = new adv_want[government_count() + 1]();
}
adv->government_want.resize(government_count());

adv->wonder_city = 0;

Expand All @@ -746,8 +744,7 @@ void adv_data_close(struct player *pplayer)

adv_data_phase_done(pplayer);

delete[] adv->government_want;
adv->government_want = nullptr;
adv->government_want.clear();
if (adv->dipl.adv_dipl_slots != nullptr) {
players_iterate(aplayer)
{
Expand Down
2 changes: 1 addition & 1 deletion server/advisors/advdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ struct adv_data {
int infra_priority;

// Government data
adv_want *government_want;
std::vector<adv_want> government_want;
short govt_reeval;

// Goals
Expand Down

0 comments on commit ce2d762

Please sign in to comment.