|
Boost Users : |
From: Robert Ramey (ramey_at_[hidden])
Date: 2006-06-09 14:23:55
I'm concerned that the input put archive is not destroyed soon enough.
It has to hold a copy of the shared pointer as long its open. Try the
following
changes: (I've simplified it a little)
int main(int argc, char **argv)
{
const char *filename = "bag.ser";
{
const Bag b(shared_ptr<Item>(new Item(1)));
// now serialize the Bag
cout << "About to serialize bag..." << endl;
ofstream osSer(filename);
boost::archive::text_oarchive oa(osSer);
oa << b;
b.printUseCount();
cout << "About to delete bag 1..." << endl;
} // no need for stream close, archive and stream closed automatically
cout << "About to create a new bag..." << endl;
Bag b1;
{
b1.printUseCount();
// and deserialize it into the new Bag object
cout << "About to deserialize bag..." << endl;
ifstream isSer(filename, std::ios::binary);
boost::archive::text_iarchive ia(isSer);
ia >> b1;
b1.printUseCount(); // should equal 2 - b1 plus one in the archive
} // close all streams and archives
b1.printUseCount(); // should equal 1 - b1 plus one in the archive
return 0;
}
I haven't tested this - if it doesn't work as expected - my name is mud
here.
Robert Ramey
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net