Boost logo

Boost Users :

From: Daniel Albuschat (d.albuschat_at_[hidden])
Date: 2007-06-12 07:30:25


Hello there,

I'm currently building my own, specialized http-server based on the http-server
example.
I experience crashes for certain types of replies. Here's something I
really do not understand:

In connection.cpp, after request_parser_.parse() finished and result
== true, there is this
line:
      boost::asio::async_write(socket_, reply_.to_buffers(),
          boost::bind(&connection::handle_write, shared_from_this(),
            boost::asio::placeholders::error));

It's the last user-code in the stacktrace of the crash that I
frequently experience.

What bothers me is the reply_.to_buffers() function. It looks like this:

std::vector<boost::asio::const_buffer> reply::to_buffers()
{
  std::vector<boost::asio::const_buffer> buffers;
  buffers.push_back(status_strings::to_buffer(status));
  for (std::size_t i = 0; i < headers.size(); ++i)
  {
    header& h = headers[i];
    buffers.push_back(boost::asio::buffer(h.name));
    buffers.push_back(boost::asio::buffer(misc_strings::name_value_separator));
    buffers.push_back(boost::asio::buffer(h.value));
    buffers.push_back(boost::asio::buffer(misc_strings::crlf));
  }
  buffers.push_back(boost::asio::buffer(misc_strings::crlf));
  buffers.push_back(boost::asio::buffer(content));
  return buffers;
}

How can this even work? It adds temporaries (the misc_strings are
const char[]s) to
a const_buffer-vector and reads them later. Even the references to h.name, etc.
seem invalid to me, since the io is asynchronous and there's no
guarantee that they don't
change.

IMHO the whole content needs to be COPIED to new buffers and then, after the
async_write finished, deleted.

Am I missing something? Was the author of this example not aware of
the const_buffer's
ownership policy (i.e. it is *not* owner of the data)?

I've spent hours and hours in debugging now, but I just can't figure
out where it goes wrong.
You can see the stacktrace here:
http://mandrill.fuxx0r.net/c++/paste:7702

Kind regards,
   Daniel Albuschat

-- 
eat(this); // delicious suicide

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