
AMDG Avi Bahra wrote:
Using latest boost version 39 on gcc 4.2.1 on SUSE linux I am trying to load a file, and then split the lines into a vector of strings. However when this is run, it showed that the last string was corrupt. When I ran this with valgrind, the very first error shows an invalid read of size 1, in guts of boost::char_separator.
<snip>
here is program:
BOOST_AUTO_TEST_CASE( test_log_append ) {
string logFile = "test/logfile.txt";
// Load the log file into a vector, of strings, and test content ifstream ifs(logFile.c_str()); BOOST_REQUIRE_MESSAGE(ifs, "Could not open log file\n");
stringstream ss; ss << ifs.rdbuf(); // Read the whole file into a string char_separator<char> sep("\n"); // Split the file content unix=\n pc =\n\r typedef boost::tokenizer<boost::char_separator<char> > tokenizer; tokenizer tokens(ss.str(), sep); // <<<<<<<< valgrind barfs here
ss.str() returns a temporary std::string. boost::tokenizer stores a reference to the string. The temporary string is destroyed and tokenizer is left with a dangling reference. In Christ, Steven Watanabe