1
0
mirror of https://github.com/OpenTTD/OpenTTD.git synced 2025-09-01 10:59:12 +00:00

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();

View File

@@ -16,7 +16,7 @@
class ClientNetworkGameSocketHandler : public ZeroedMemoryAllocator, public NetworkGameSocketHandler {
private:
std::string connection_string; ///< Address we are connected to.
struct PacketReader *savegame; ///< Packet reader for reading the savegame.
std::shared_ptr<struct PacketReader> savegame; ///< Packet reader for reading the savegame.
byte token; ///< The token we need to send back to the server to prove we're the right client.
/** Status of the connection with the server. */

View File

@@ -576,7 +576,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendMap()
Debug(net, 9, "client[{}] SendMap(): first_packet", this->client_id);
WaitTillSaved();
this->savegame = new PacketWriter(this);
this->savegame = std::make_shared<PacketWriter>(this);
/* Now send the _frame_counter and how many packets are coming */
Packet *p = new Packet(PACKET_SERVER_MAP_BEGIN);

View File

@@ -69,7 +69,7 @@ public:
CommandQueue outgoing_queue; ///< The command-queue awaiting delivery
size_t receive_limit; ///< Amount of bytes that we can receive at this moment
struct PacketWriter *savegame; ///< Writer used to write the savegame.
std::shared_ptr<struct PacketWriter> savegame; ///< Writer used to write the savegame.
NetworkAddress client_address; ///< IP-address of the client (so they can be banned)
ServerNetworkGameSocketHandler(SOCKET s);