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: Liviu <liviu_c_dorobantu@…> 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?

Change History (1)

comment:1 by Robert Ramey, 10 years ago

Resolution: invalid
Status: newclosed

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:

#include <boost/archive/binary_oarchive.hpp> 
#include <boost/archive/binary_iarchive.hpp> 
#include <boost/serialization/string.hpp>
#include <boost/serialization/vector.hpp> // note:use standard vector serialization !
#include <iostream> 
#include <string> 
#include <fstream>

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);
    oa << v;
    file.close();
}; 

void load() 
{ 
    ifstream file("archiv.cst"); 
    boost::archive::binary_iarchive ia(file); 
    ia >> v;
    file.close();
};

int main() 
{
    int i;

    for (i=1; i<=COUNT; i++){
        v.push_back(person("John", i));
    }

    cin>>i;
    while (i!=2)
    {
        if (i==0) save();
        if (i==1) load();
        cin>>i;
    }
} 

and see how that works. I think I'm correct, so I'm going to close this item for now.

Robert Ramey

Note: See TracTickets for help on using tickets.