Smart pointers with addrinfo struct

帅比萌擦擦* 提交于 2019-12-07 12:25:29

问题


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:

  1. addrinfo is an "old" C structure, with no ctor/dtor to call: is it safe to use new?
  2. getaddrinfo requires a pointer to a pointer to a addrinfo struct: how should I pass it via smart pointers?
  3. What about calling freeaddrinfo? It's considered unsafe to delete (or better free) 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!