From 187a3f20bfd7578362f666b869580bafe25ffd0b Mon Sep 17 00:00:00 2001 From: Rubidium Date: Wed, 12 May 2021 23:06:35 +0200 Subject: [PATCH] Codechange: remove pointless close call due to resolving virtual functions statically in destructors In the destructors of many of the network related classes Close() is called, just like the top class in that hierarchy. However, due to virtual functions getting resolved statically in the destructor it would always call the empty Close() of the top class. Document the other cases where a virtual call is resolved statically. --- src/network/core/core.h | 5 +---- src/network/core/tcp.cpp | 3 ++- src/network/core/tcp_content.cpp | 6 ++++-- src/network/core/tcp_content.h | 8 ++++++-- src/network/core/udp.h | 2 +- src/network/network_content.cpp | 4 +++- src/network/network_content.h | 2 +- 7 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/network/core/core.h b/src/network/core/core.h index 37948ad527..a16ed9f23b 100644 --- a/src/network/core/core.h +++ b/src/network/core/core.h @@ -46,10 +46,7 @@ public: NetworkSocketHandler() { this->has_quit = false; } /** Close the socket when destructing the socket handler */ - virtual ~NetworkSocketHandler() { this->Close(); } - - /** Really close the socket */ - virtual void Close() {} + virtual ~NetworkSocketHandler() {} /** * Close the current connection; for TCP this will be mostly equivalent diff --git a/src/network/core/tcp.cpp b/src/network/core/tcp.cpp index 3bba291c73..5c436edf05 100644 --- a/src/network/core/tcp.cpp +++ b/src/network/core/tcp.cpp @@ -29,7 +29,8 @@ NetworkTCPSocketHandler::NetworkTCPSocketHandler(SOCKET s) : NetworkTCPSocketHandler::~NetworkTCPSocketHandler() { - this->CloseConnection(); + /* Virtual functions get called statically in destructors, so make it explicit to remove any confusion. */ + this->NetworkTCPSocketHandler::CloseConnection(); if (this->sock != INVALID_SOCKET) closesocket(this->sock); this->sock = INVALID_SOCKET; diff --git a/src/network/core/tcp_content.cpp b/src/network/core/tcp_content.cpp index 0371b76215..3abf1c29c9 100644 --- a/src/network/core/tcp_content.cpp +++ b/src/network/core/tcp_content.cpp @@ -137,9 +137,11 @@ const char *ContentInfo::GetTextfile(TextfileType type) const return ::GetTextfile(type, GetContentInfoSubDir(this->type), tmp); } -void NetworkContentSocketHandler::Close() +/** + * Close the actual socket. + */ +void NetworkContentSocketHandler::CloseSocket() { - CloseConnection(); if (this->sock == INVALID_SOCKET) return; closesocket(this->sock); diff --git a/src/network/core/tcp_content.h b/src/network/core/tcp_content.h index 52cae1e0ed..b1bde48172 100644 --- a/src/network/core/tcp_content.h +++ b/src/network/core/tcp_content.h @@ -21,7 +21,7 @@ /** Base socket handler for all Content TCP sockets */ class NetworkContentSocketHandler : public NetworkTCPSocketHandler { protected: - void Close() override; + void CloseSocket(); bool ReceiveInvalidPacket(PacketContentType type); @@ -124,7 +124,11 @@ public: } /** On destructing of this class, the socket needs to be closed */ - virtual ~NetworkContentSocketHandler() { this->Close(); } + virtual ~NetworkContentSocketHandler() + { + /* Virtual functions get called statically in destructors, so make it explicit to remove any confusion. */ + this->CloseSocket(); + } bool ReceivePackets(); }; diff --git a/src/network/core/udp.h b/src/network/core/udp.h index 881fb0a612..ab898eeee3 100644 --- a/src/network/core/udp.h +++ b/src/network/core/udp.h @@ -190,7 +190,7 @@ public: virtual ~NetworkUDPSocketHandler() { this->Close(); } bool Listen(); - void Close() override; + void Close(); void SendPacket(Packet *p, NetworkAddress *recv, bool all = false, bool broadcast = false); void ReceivePackets(); diff --git a/src/network/network_content.cpp b/src/network/network_content.cpp index e4f368618b..26d220b6ae 100644 --- a/src/network/network_content.cpp +++ b/src/network/network_content.cpp @@ -784,7 +784,9 @@ void ClientNetworkContentSocketHandler::Connect() void ClientNetworkContentSocketHandler::Close() { if (this->sock == INVALID_SOCKET) return; - NetworkContentSocketHandler::Close(); + + this->CloseConnection(); + this->CloseSocket(); this->OnDisconnect(); } diff --git a/src/network/network_content.h b/src/network/network_content.h index f28821a0bf..13b93417c3 100644 --- a/src/network/network_content.h +++ b/src/network/network_content.h @@ -107,7 +107,7 @@ public: void Connect(); void SendReceive(); - void Close() override; + void Close(); void RequestContentList(ContentType type); void RequestContentList(uint count, const ContentID *content_ids);