Compiler error or speed up library?

This program (it was narrowed down from a larger program) is always reset after compilation in version release mode (Win32) vs2008 under windows 7. I am not familiar with the assembly code and do not know that this is a compiler or boost :: ends_with or boost :: error asio :: buffers_iterator. It can be compiled and executed using g ++ on Ubuntu without any problems.

People said that it is very unlikely that this is a compiler error, but when it is compiled during debugging (or disabling optimization), the problem disappears.

I am stuck with this problem for several hours. Any help is appreciated. Thanks in advance.

#include <iostream> #include <string> #include <boost/asio.hpp> #include <boost/algorithm/string.hpp> typedef boost::asio::buffers_iterator<boost::asio::const_buffers_1> iterator_t; typedef boost::iterator_range<iterator_t> range_t; static const std::string LINE_END_MARK = "\r\n"; int main(int argc, char* argv[]) { boost::asio::streambuf _buf; std::ostream os(&_buf); os<<"END\r\n"; iterator_t cursor = boost::asio::buffers_begin(_buf.data()); iterator_t end = boost::asio::buffers_end(_buf.data()); std::ostream_iterator<char> it(std::cout," "); std::copy(LINE_END_MARK.begin(), LINE_END_MARK.end(), it); range_t r(cursor, end); if(!boost::ends_with(r, LINE_END_MARK)) return 0; return 1; } 
+8
c ++ boost visual-c ++ boost-asio
source share
2 answers

Edit: I am not reading the code correctly, sorry.

The cursor and end icons indicate an invalid memory. You changed the underlying streambuf that was redistributed during copying to the output iterator. asio streambuf allows you to access raw memory for performance reasons, but the caveat is that you need to worry about such things.

Debugging and release will change the way you manage allocation and deallocation in relation to the base size of the allocated block and how memory can be fenced, protected, initialized, aligned, etc.

Build your iterators after the copy operation to fix your problem.

+1
source share

This does not work because 'range_t r (cursor, end)' is a range of buffers, not a range of characters. Thus, you are comparing a list of buffer pointers with each character in LINE_END_MARK.

If a crash occurs in release mode under win32, because on Windows you end up deleting the reference to the null pointer that causes the crash.

boost asio has this concept of multiple buffers, but is currently not used. If you look at the implementation, if only it actually uses "const_buffers_1" or "mutable_buffers_1", which is basically a list of 1 buffer.

I assume that you want to compare the contents of the buffer, not the list of buffer ranges.

So you want to do something like:

 typedef boost::iterator_range<const char*> range_t; range_t r(boost::asio::buffer_cast<const char*>(_buf.data()), boost::asio::buffer_cast<const char*>(_buf.data()) + boost::asio::buffer_size(_buf.data())); if(!boost::ends_with(r, LINE_END_MARK)) return 0; return 1; 
+1
source share

All Articles