Commit 0620c8e5 authored by louiz’'s avatar louiz’

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.
parent 5027b21f
......@@ -44,8 +44,16 @@ TCPSocketHandler::TCPSocketHandler(std::shared_ptr<Poller> poller):
#endif
{}
TCPSocketHandler::~TCPSocketHandler()
{
this->close();
}
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)
throw std::runtime_error("Could not create socket: "s + strerror(errno));
int optval = 1;
......
......@@ -27,7 +27,7 @@
class TCPSocketHandler: public SocketHandler
{
protected:
~TCPSocketHandler() = default;
~TCPSocketHandler();
public:
explicit TCPSocketHandler(std::shared_ptr<Poller> poller);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment