Boost logo

Boost Users :

Subject: Re: [Boost-users] Serialization of boost::shared_ptr - wrong ref. count?
From: Robert Ramey (ramey_at_[hidden])
Date: 2015-04-14 00:21:05


I ran a modified version of this program on my Mac OS system and it worked as
I would expect. I'm running the latest version of the serialization library

Robert Ramey

my version:

//#include <boost/iostreams/device/file_descriptor.hpp>
//#include <boost/iostreams/filtering_stream.hpp>
#include <fstream>
#include "test_tools.hpp"
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/make_shared.hpp>

class SOBJ {
public:
    friend class boost::serialization::access;
    virtual void SerializationHelperFcn() {}
    SOBJ() {
        std::cout << "SOBJ Constructor" << std::endl;
    }

    virtual ~SOBJ() {
        std::cout << "SOBJ Destructor" << std::endl;
    }
    double value=0.;
    template<typename Archive> void serialize(Archive &ar, const unsigned
int ver) {
        ar & boost::serialization::make_nvp("value", value);
    }
};

void shared_ptr_serialization_test() {
    const char * testfile = boost::archive::tmpnam(NULL);
    boost::shared_ptr<SOBJ> out=boost::make_shared<SOBJ>();

    {
        //boost::iostreams::filtering_ostream ofs;
       
//ofs.push(boost::iostreams::file_descriptor_sink("/Users/juhe/stest.xml"));
        std::ofstream ofs(testfile);
        boost::archive::xml_oarchive oa(ofs);
        oa << boost::serialization::make_nvp("tag", out);
    }

    boost::shared_ptr<SOBJ> in=nullptr;

    {
// boost::iostreams::filtering_istream ifs;
//
ifs.push(boost::iostreams::file_descriptor_source("/Users/juhe/stest.xml"));
        std::ifstream ifs(testfile);
        boost::archive::xml_iarchive ia(ifs);
        ia >> boost::serialization::make_nvp("tag", in);
    }

    std::cout << "out.use_count()=" << out.use_count() << std::endl;
    std::cout << "in.use_count() =" << in.use_count() << std::endl;
}

int test_main(int argc, char * argv[]){
    shared_ptr_serialization_test();
    return 0;
}
/raw>

--
View this message in context: http://boost.2283326.n4.nabble.com/Serialization-of-boost-shared-ptr-wrong-ref-count-tp4674427p4674440.html
Sent from the Boost - Users mailing list archive at Nabble.com.

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