tsotso
tsotso

Reputation: 352

Using boost::shared_ptr with WSACleanup

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?

Upvotes: 1

Views: 250

Answers (2)

MSalters
MSalters

Reputation: 180235

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();});

Upvotes: 1

Tio Pepe
Tio Pepe

Reputation: 3099

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);
}

Upvotes: 1

Related Questions