Using boost::shared_ptr with WSACleanup

246 views Asked by At

I would like to use a boost::shared_ptr in order for WSACleanup() to be called when my function goes out of scope, like this:

void DoSomething() {
    WSAStartup(...);
    boost::shared_ptr<void> WSACleaner(static_cast<void*>(0), WSACleanup);
}

This does not compile,

Error 1 error C2197: 'int (__stdcall *)(void)' : too many arguments for call C:\projects\svn-5.3\ESA\Common\include\boost\detail\shared_count.hpp 116

any thoughts?

2

There are 2 answers

8
MSalters On BEST ANSWER

From the docs: "The expression d(p) must be well-formed" (i.e. WSACleanup(static_cast<void*>(0) must be well-formed.)

One possible solution:

boost::shared_ptr<void> WSACleaner(static_cast<void*>(0),
                                   [](void* dummy){WSACleanup();});
3
Tio Pepe On

You can create a class Awhich destructor invoques WSACleanup and instance of shared_ptr with it:

class A
{
    public:
        ~A() { WSACleanup(...); }
}

....

void DoSomething() {
    WSAStartup(...);
    boost::shared_ptr<A> x(new A);
}