boost asio streambuf don't release memory after calling consume?

后端 未结 1 1657
情歌与酒
情歌与酒 2021-01-14 01:59
boost::asio::streambuf b;
...
void handler(const boost::system::error_code& e, std::size_t size)
{
  if (!e)
  {
    std::stringstream sstr(std::string((std::ist         


        
相关标签:
1条回答
  • 2021-01-14 02:29

    asio::streambuf is based on std::vector that grows as needed, but never shrinks. So, consume() is not supposed to release memory, it just adjusts internal pointers:

    void consume(std::size_t n)
    {
      if (egptr() < pptr())
        setg(&buffer_[0], gptr(), pptr());
      if (gptr() + n > pptr())
        n = pptr() - gptr();
      gbump(static_cast<int>(n));
    }
    

    But each time you consume() and read() again, the internal buffer (vector) is reused, so you don't need to release anything.

    0 讨论(0)
提交回复
热议问题