[Serialize] Problem creating boost::archive::text_iarchive
Hello, I'm getting problems creating a boost::archive::text_iarchive with boosts version 1.35.0 and Visual C++ Studio 2005 (Version 8). When I do so I get a std::bad_alloc exception. The code is as follows: void Serialize::deserialize(std::ifstream &modelFile, CoreModel &c) { try { boost::archive::text_iarchive ia(modelFile); // This line throws the exception (I've seen it with the debugger) ia >> c; } catch (std::exception& ex) { // I catch the bad_alloc here } } And this code si called this way: ... for (...) { modelFile.open(model_name.c_str(), std::ios::binary); if (modelFile.is_open() == false) { // ... We deal with the error but it does not get into here } CoreModel model; Serialize::deserialize(modelFile, model); .... } With the debugger I have seen that the exception is thrown into the method text_iarchive_impl<Archive>::load. The method basic_text_iprimitive::load is not setting the size of the string and when the string resize is done the value is huge (is garbage actually) and it throws the std::bad_alloc. Digging into the mailing list I have seen a thread that explains the same problem: http://lists.boost.org/boost-users/2006/03/18032.php But the solution proposed there does not fit me (As far as I can see). /Thanks Francisco Javier Sanz Olviera
One thing odd about your example is opening the text archive stream with "ios::binary". Do you know for a fact that the archive was created this way?
Robert Ramey
"Javier Sanz"
It seems that find_format_all() calls the formatter with an invalid match when the end of the input is reached. Is this a bug? Am I missing something? Example: // A Boost.StringAlgo Formatter expecting to be called for regex matches for (\d+\.)(\d+) struct NextVersionNumber { template<class RegexMatchT> std::string operator()(const RegexMatchT& M)const { // Without this check, we end up with a bad_lexical_cast exception, // because operator() ends up being called twice: // First with a good match, and then with an emtpy one if (!Match){ return ""; } using boost::lexical_cast; return M.match_results()[1] + lexical_caststd::string(lexical_cast<unsigned int>(M.match_results()[2]) + 1); } }; int main() { namespace bsa = boost::algorithm; const std::string s("version is 1.0"); // Outputs: // Next version is: 1.1 std::cout << "Next version is: " << bsa::find_format_all_copy( s, bsa::regex_finder(boost::regex("(\\d+\\.)(\\d+)")), NextVersionNumber() ); return 0; }
participants (3)
-
Eric MALENFANT
-
Javier Sanz
-
Robert Ramey