Codechange: use std::shared_ptr to manage saveload filters instead of manually trying to avoid double frees

This commit is contained in:
Rubidium
2024-02-02 18:13:38 +01:00
committed by rubidium42
parent 22eed9616e
commit 4b372b6050
8 changed files with 40 additions and 59 deletions

View File

@@ -155,7 +155,6 @@ ClientNetworkGameSocketHandler::~ClientNetworkGameSocketHandler()
assert(ClientNetworkGameSocketHandler::my_client == this);
ClientNetworkGameSocketHandler::my_client = nullptr;
delete this->savegame;
delete this->GetInfo();
}
@@ -567,7 +566,7 @@ bool ClientNetworkGameSocketHandler::IsConnected()
* DEF_CLIENT_RECEIVE_COMMAND has parameter: Packet *p
************/
extern bool SafeLoad(const std::string &filename, SaveLoadOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = nullptr);
extern bool SafeLoad(const std::string &filename, SaveLoadOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, std::shared_ptr<struct LoadFilter> lf);
NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_FULL(Packet *)
{
@@ -810,7 +809,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_BEGIN(Packe
if (this->savegame != nullptr) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
this->savegame = new PacketReader();
this->savegame = std::make_shared<PacketReader>();
_frame_counter = _frame_counter_server = _frame_counter_max = p->Recv_uint32();
@@ -864,20 +863,12 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DONE(Packet
_network_join_status = NETWORK_JOIN_STATUS_PROCESSING;
SetWindowDirty(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_JOIN);
/*
* Make sure everything is set for reading.
*
* We need the local copy and reset this->savegame because when
* loading fails the network gets reset upon loading the intro
* game, which would cause us to free this->savegame twice.
*/
LoadFilter *lf = this->savegame;
this->savegame = nullptr;
lf->Reset();
this->savegame->Reset();
/* The map is done downloading, load it */
ClearErrorMessages();
bool load_success = SafeLoad({}, SLO_LOAD, DFT_GAME_FILE, GM_NORMAL, NO_DIRECTORY, lf);
bool load_success = SafeLoad({}, SLO_LOAD, DFT_GAME_FILE, GM_NORMAL, NO_DIRECTORY, this->savegame);
this->savegame = nullptr;
/* Long savegame loads shouldn't affect the lag calculation! */
this->last_packet = std::chrono::steady_clock::now();