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:
Florent Le Coz
2015-11-07 23:41:06 +01:00
parent 5027b21fd1
commit 0620c8e589
2 changed files with 9 additions and 1 deletions
@@ -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;
+1 -1
View File
@@ -27,7 +27,7 @@
class TCPSocketHandler: public SocketHandler
{
protected:
~TCPSocketHandler() = default;
~TCPSocketHandler();
public:
explicit TCPSocketHandler(std::shared_ptr<Poller> poller);