Opened 10 years ago
Closed 10 years ago
#6850 closed Bugs (invalid)
serialization/deserialization does not work for more than 25 objects (very strange!!!)
Reported by: | Owned by: | Robert Ramey | |
---|---|---|---|
Milestone: | To Be Determined | Component: | serialization |
Version: | Boost 1.47.0 | Severity: | Problem |
Keywords: | serialization deserialization 25 | Cc: |
Description
hello I have installed boost 1.47.0 library on my system and I have a strange problem. If I try to serialize more then 25 objects, the serialization (save()) apparently works but the deserialization (load()) does not work anymore and it gives me the following error: Unhandled exception at 0x7c812afb in Boost_tests.exe: Microsoft C++ exception: boost::archive::archive_exception at memory location 0x0012e5ec.. this error is raised when the last object (say the 26th object, if the number of objects is higher then 25) is being deserialized. here is the code:
#include <boost/archive/binary_oarchive.hpp> #include <boost/archive/binary_iarchive.hpp> #include <boost/serialization/string.hpp> #include <iostream> #include <string> #include <fstream> #include <vector> using namespace std; #define COUNT 26 class person; vector<person> v; class person { public: string _name; int _age; person (string name="Liviu", int age=25):_name(name),_age(age){}; friend class boost::serialization::access; template <typename Archive> void serialize(Archive & ar, const unsigned int version) { ar & _name; ar & _age; } }; void save() { ofstream file("archiv.cst"); boost::archive::binary_oarchive oa(file); for (int i=1; i<=COUNT; i++) { oa<<person("John",i); } file.close(); }; void load() { ifstream file("archiv.cst"); boost::archive::binary_iarchive ia(file); for (int i=1; i<=COUNT; i++) { person p; ia>>p; v.push_back(p); // cout << p._name<<" " <<p._age<<endl; } file.close(); }; int main() { int i; cin>>i; while (i!=2) { if (i==0) save(); if (i==1) load(); cin>>i; } }
Can you help me please? Is something wrong with the library?
the default std::vector size is some number. You're not overriding that when oyu rebuild. You might want to look into how std:vector is serialized. I believe you should let the standard code in the library do this for you. That is, you should change your code to:
and see how that works. I think I'm correct, so I'm going to close this item for now.
Robert Ramey