Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::asio::buffer exception
From: Bill Somerville (bill_at_[hidden])
Date: 2010-11-12 12:24:27


Lloyd Kanakkassery wrote:
> Hi,
>
> I want to write some data using boost::asio::write(). write() has two
> arguments, one is the stream to which the data to be written and the
> other is the data buffer. The data buffer can be created using
> boost::asio::buffer() function. When I use it this way it works. Then,
> I have tried to wrap boost::asio::bufffer() in my custom function like
> this
>
> boost::asio::const_buffers_1 MyBuffer(string msg)
> {
> return boost::asio::const_buffers_1(boost::asio::const_buffer(msg.c_str(),msg.length()));
> }
>
> The I use this in my asio::write() as
>
> booost::asio::write(mySocket, MyBuffer("hai"));
>
You are passing a string by value to your function and returning a
pointer to its guts. The string needs to have at least as long a
lifetime as the write operation.
> This program compiles and runs but it throws an exception called "The
> system detected an invalid pointer address in attempting to use a
> pointer argument in a call". I understand that, it is associated with
> the MyBuffer() function. I have checked it with the buffer() function
> available in "buffer.hpp". Both are written in the same way, but my
> function is throwing exception. Could you give some hint?
>
> Thanks and Regards,
> Lloyd
>
HTH
Bill Somerville.


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