I need to deal with two struct addrinfo
pointers. Since I'm coding in C++(11), I've to make my code exception-safe. Indeed, my costructors may throw a runtime_error
.
When you don't need that kind of struct anymore, you should call freeaddrinfo
in order to free the list inside the struct. Please consider the following code:
#include <memory>
#include <netdb.h>
class SomeOtherClass
{
public:
SomeOtherClass() : hints(new addrinfo), result(new addrinfo) { /*stuff*/ }
~SomeOtherClass() { freeaddrinfo(result.get()); } // bad things will happen
private:
std::unique_ptr<addrinfo> hints, result;
};
class MyClass : public SomeOtherClass
{
public:
MyClass() { /* hints initialization, call to getaddrinfo, etc. */ }
private:
// ...
};
My questions are:
addrinfo
is an "old" C structure, with no ctor/dtor to call: is it safe to use new?getaddrinfo
requires a pointer to a pointer to aaddrinfo
struct: how should I pass it via smart pointers?- What about calling
freeaddrinfo
? It's considered unsafe to delete (or betterfree
) the pointer that the smart pointer is holding.
For hints
there is no problem, since its lifetime is smaller.