Boost logo

Boost Users :

Subject: Re: [Boost-users] [Asio] Is shared_ptr with async_send_to ok?
From: Igor R (boost.lists_at_[hidden])
Date: 2009-11-11 04:04:21


> Socket.async_send_to(boost::asio::buffer(*SharedBuffer, BufferSize), DestinationEndpoint, boost::bind(&MyClass::OnPacketSent, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
> // Shared buffer goes out of scope here, immediately!

You do not copy SharedBuffer, so it's destroyed when goes out of
scope. If you want SharedBuffer to outlive the handler, you can store
the shared_ptr within the handler, like this:

// UNTESTED!
using boost::bind;
using boost::protect;
Socket.async_send_to(buffer(*SharedBuffer, BufferSize), DestinationEndpoint,
bind(protect(bind(&MyClass::OnPacketSent, this, _1, _2)), _1, _2,
SharedBuffer)); // you can revert back to the asio placeholders


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net