Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bug of compiler or boost library?

This program (it has been narrowed down from a larger program) always crashes after compiled in vs2008 Release(Win32) mode under windows 7. I am not familiar with assembly code and don't know it's a bug of compiler or boost::ends_with or boost::asio::buffers_iterator. It can be compiled and executed with g++ in Ubuntu without any problem.

People said it's very unlikely to be compiler's bug, but when compiled in debug moded(or disable optimization), the problem does disappear.

I have been stuck with this problem for quite a few 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;
}
like image 820
user869210 Avatar asked Oct 31 '12 08:10

user869210


1 Answers

Edit: I misread the code, sorry.

Your cursor and end iterators are pointing to invalid memory. You modified the underlying streambuf which reallocated during the copy to the output iterator. asio streambuf lets you access the raw memory for performance reasons, but the caveat is you have to worry about things like this.

Debug and release will change the way allocation and deallocation behave with regards to the underlying size of the allocated block and how memory may be fenced, guarded, initialized, aligned, etc.

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

like image 195
adzm Avatar answered Nov 15 '22 12:11

adzm