问题
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:
addrinfois an "old" C structure, with no ctor/dtor to call: is it safe to use new?getaddrinforequires a pointer to a pointer to aaddrinfostruct: 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.
回答1:
For any addrinfo you allocate yourself, it is safe to use newand delete, so you can use the default implementation of unique_ptr to handle that.
For any addrinfo that getaddrinfo() allocates, you must use freeaddrinfo() to free it. You can still use unique_ptr for that, but you must specify freeaddrinfo() as a custom Deleter, eg:
class SomeOtherClass
{
public:
SomeOtherClass() : hints(new addrinfo), result(nullptr, &freeaddrinfo) { /*stuff*/ }
private:
std::unique_ptr<addrinfo> hints;
std::unique_ptr<addrinfo, void(__stdcall*)(addrinfo*)> result;
};
Then you can do this:
getaddrinfo(..., &result);
Or this, if std::unique_ptr does not override the & operator:
addrinfo *temp;
getaddrinfo(..., &temp);
result.reset(temp);
UPDATE: a better option is to use decltype and let the compiler deduce the function type of the Deleter for you:
std::unique_ptr<addrinfo, decltype(&freeaddrinfo)> result;
来源:https://stackoverflow.com/questions/21942280/smart-pointers-with-addrinfo-struct