diff options
author | Florent Le Coz <louiz@louiz.org> | 2015-11-07 23:41:06 +0100 |
---|---|---|
committer | Florent Le Coz <louiz@louiz.org> | 2015-11-07 23:41:06 +0100 |
commit | 0620c8e589c713fa0f0d0f66a4c4d203e8f3f87f (patch) | |
tree | 30c4aaf1b49036102ebd6e3805af0bac5695c213 | |
parent | 5027b21fd1fe0dec6a82a2b0f428f30f5cc98a42 (diff) | |
download | biboumi-0620c8e589c713fa0f0d0f66a4c4d203e8f3f87f.tar.gz biboumi-0620c8e589c713fa0f0d0f66a4c4d203e8f3f87f.tar.bz2 biboumi-0620c8e589c713fa0f0d0f66a4c4d203e8f3f87f.tar.xz biboumi-0620c8e589c713fa0f0d0f66a4c4d203e8f3f87f.zip |
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.
-rw-r--r-- | louloulibs/network/tcp_socket_handler.cpp | 8 | ||||
-rw-r--r-- | louloulibs/network/tcp_socket_handler.hpp | 2 |
2 files changed, 9 insertions, 1 deletions
diff --git a/louloulibs/network/tcp_socket_handler.cpp b/louloulibs/network/tcp_socket_handler.cpp index 0ed74a2..d5c0dfa 100644 --- a/louloulibs/network/tcp_socket_handler.cpp +++ b/louloulibs/network/tcp_socket_handler.cpp @@ -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; diff --git a/louloulibs/network/tcp_socket_handler.hpp b/louloulibs/network/tcp_socket_handler.hpp index 213e286..d33b919 100644 --- a/louloulibs/network/tcp_socket_handler.hpp +++ b/louloulibs/network/tcp_socket_handler.hpp @@ -27,7 +27,7 @@ class TCPSocketHandler: public SocketHandler { protected: - ~TCPSocketHandler() = default; + ~TCPSocketHandler(); public: explicit TCPSocketHandler(std::shared_ptr<Poller> poller); |