Boost asio streambuf not freeing memory after call?

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 consume , the memory occupied by streambuf b is not freed. Memory will grow async_read_until is called several times. Is my use correct? Is there a way to free memory before the get pointer streambuf?

+4
source share
1 answer

asio :: streambuf is based on std :: vector, which grows as needed, but never shrinks. So consumume () should not release memory, it just sets up 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 every time you consume () and read () again, the internal buffer (vector) is reused, so you don't have to let go of anything.

+4
source

Source: https://habr.com/ru/post/1399616/


All Articles