Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force boost::asio::buffer to copy by value

Tags:

c++

boost-asio

I use boost::asio::buffer to send a message using

void Send(const std::string& messageData)
{
    socket.async_write(boost::asio::buffer(messageData), ...);
}

And encounter "string iterator not dereferencable" runtime error somewhere within io_service' thread. When I create objects' variable to store message data for the buffer:

void Send(const std::string& messageData)
{
    this->tempStorage = messageData;
    socket.async_write(boost::asio::buffer(this->tempStorage), ...);
}

the error is never occured. std::string (to which messageData is referenced to) is freed almost right after Send() calling - does boost::asio::buffer stores just a reference to object? If so, how can I force it to store the data by value?

like image 355
Slaus Avatar asked May 02 '11 19:05

Slaus


2 Answers

Answer from boost-users mailing lists:

If it did, it would be completely useless as you do not have access to any of the buffers in your completion handler.

The way to use buffer() is to pass in references to storage that you guarantee the lifetime of in some other way.

You can either have it stored in an external object, or store it in `this' as you did, or by binding it into the completion handler function object itself.

void onComplete(shared_ptr<std::string> s, error_code const&, size_t)
{
    // do stuff
}

void send(std::string const& messageData)
{
    shared_ptr<std::string> s = make_shared<std::string>(messageData);
    async_send(socket, boost::asio::buffer(*s),
    boost::bind(&T::onSend, this, s, _1, _2));
}

This ensures that the lifetime of the buffer data is at least as long as the completion handler exists.

like image 64
Slaus Avatar answered Sep 27 '22 23:09

Slaus


From boost::asio's documentation:

A buffer object does not have any ownership of the memory it refers to. It is the responsibility of the application to ensure the memory region remains valid until it is no longer required for an I/O operation. When the memory is no longer available, the buffer is said to have been invalidated.

For the boost::asio::buffer overloads that accept an argument of type std::vector, the buffer objects returned are invalidated by any vector operation that also invalidates all references, pointers and iterators referring to the elements in the sequence (C++ Std, 23.2.4)

For the boost::asio::buffer overloads that accept an argument of type std::basic_string, the buffer objects returned are invalidated according to the rules defined for invalidation of references, pointers and iterators referring to elements of the sequence (C++ Std, 21.3).

like image 39
billow Avatar answered Sep 27 '22 23:09

billow