Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7588171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:51:25+00:00 2026-05-30T19:51:25+00:00

boost::asio::streambuf b; … void handler(const boost::system::error_code& e, std::size_t size) { if (!e) { std::stringstream

  • 0
boost::asio::streambuf b;
...
void handler(const boost::system::error_code& e, std::size_t size)
{
  if (!e)
  {
    std::stringstream sstr(std::string((std::istreambuf_iterator<char>(&b)), 
        std::istreambuf_iterator<char>()));
    b.consume(size);
    ...
  }
}
...
boost::asio::async_read_until(s, b, "END\r\n", handler);

when the consume method is called, the memory occupied by streambuf b is not released. The memory will grow up as async_read_until is called multiple times. Is my usage correct? Is there any way to free the memory before the get pointer of streambuf?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:51:27+00:00Added an answer on May 30, 2026 at 7:51 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use boost::asio::buffer to send a message using void Send(const std::string& messageData) { socket.async_write(boost::asio::buffer(messageData),
I have the following code in ANSII: boost::asio::streambuf buffer; std::ostream oss(&buffer); boost::asio::async_write(socket_, buffer, strand_.wrap(
I am using the following methods: boost::asio::streambuf io_bufstream; boost::asio::async_read_until(socket_, io_bufstream, </message>, strand_.wrap( boost::bind(&Connection::handleRead, shared_from_this(),
I am trying to do the following with wide characters: boost::asio::streambuf io_streambuf_; std::iostream io_stream(io_streambuf_);
I'm using boost::asio, and I have code like this: void CServer::Start(int port) { tcp::acceptor
I use boost::asio as a network framework. As a read/write medium it uses boost::asio::streambuf
How can I flush data from streambuf to file? I've tried read(*socket_, streamBuf, boost::asio::transfer_at_least(694784))
Using g++ and boost::asio, I'm trying to format network message frames containing the size
I've noticed that boost.asio has a lot of examples involving sockets, serial ports, and
I have read the boost asio reference, gone through the tutorial and looked at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.