diff --git a/include/pistache/common.h b/include/pistache/common.h index aca1e3850..0347e76fb 100644 --- a/include/pistache/common.h +++ b/include/pistache/common.h @@ -53,6 +53,24 @@ #define UNUSED(x) (void)(x); namespace Pistache { + +// RAII class for address info +class AddressInfo { +public: + AddressInfo(const char* host, const char* port, + const struct addrinfo& hints, struct addrinfo**addrs) { + addressInfo = addrs; + TRY(::getaddrinfo(host, port, &hints, addressInfo)); + } + + ~AddressInfo() { + freeaddrinfo(*addressInfo); + } + +private: + struct addrinfo **addressInfo; +}; + namespace Const { static constexpr int MaxBacklog = 128; diff --git a/include/pistache/mailbox.h b/include/pistache/mailbox.h index 84f1d9700..1b48ae227 100644 --- a/include/pistache/mailbox.h +++ b/include/pistache/mailbox.h @@ -183,6 +183,7 @@ class Queue { virtual ~Queue() { while (auto *e = pop()) delete e; + delete tail; } template diff --git a/src/client/client.cc b/src/client/client.cc index 6bda30b2b..2101e1bdf 100644 --- a/src/client/client.cc +++ b/src/client/client.cc @@ -386,7 +386,7 @@ Connection::connect(Address addr) const auto& host = addr.host(); const auto& port = addr.port().toString(); - TRY(::getaddrinfo(host.c_str(), port.c_str(), &hints, &addrs)); + AddressInfo addressInfo(host.c_str(), port.c_str(), hints, &addrs); int sfd = -1; diff --git a/src/server/listener.cc b/src/server/listener.cc index 1901d3a14..a549db467 100644 --- a/src/server/listener.cc +++ b/src/server/listener.cc @@ -156,7 +156,7 @@ Listener::bind(const Address& address) { const auto& host = addr_.host(); const auto& port = addr_.port().toString(); struct addrinfo *addrs; - TRY(::getaddrinfo(host.c_str(), port.c_str(), &hints, &addrs)); + AddressInfo addressInfo(host.c_str(), port.c_str(), hints, &addrs); int fd = -1;