Avoid leaking socket filedescriptors
When trying the various results of getaddrinfo, we forgot to close the socket when one fails, before trying the next one. Also use the destructor to make sure we do not have some other unrelated leak.
This commit is contained in:
@@ -44,8 +44,16 @@ TCPSocketHandler::TCPSocketHandler(std::shared_ptr<Poller> poller):
|
|||||||
#endif
|
#endif
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
TCPSocketHandler::~TCPSocketHandler()
|
||||||
|
{
|
||||||
|
this->close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void TCPSocketHandler::init_socket(const struct addrinfo* rp)
|
void TCPSocketHandler::init_socket(const struct addrinfo* rp)
|
||||||
{
|
{
|
||||||
|
if (this->socket != -1)
|
||||||
|
::close(this->socket);
|
||||||
if ((this->socket = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) == -1)
|
if ((this->socket = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) == -1)
|
||||||
throw std::runtime_error("Could not create socket: "s + strerror(errno));
|
throw std::runtime_error("Could not create socket: "s + strerror(errno));
|
||||||
int optval = 1;
|
int optval = 1;
|
||||||
|
|||||||
@@ -27,7 +27,7 @@
|
|||||||
class TCPSocketHandler: public SocketHandler
|
class TCPSocketHandler: public SocketHandler
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
~TCPSocketHandler() = default;
|
~TCPSocketHandler();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit TCPSocketHandler(std::shared_ptr<Poller> poller);
|
explicit TCPSocketHandler(std::shared_ptr<Poller> poller);
|
||||||
|
|||||||
Reference in New Issue
Block a user