Hi All,
I am using serialisation of boost::shared_ptr objects. Saving/loading works fine, except that I found out that all shared_ptr objects have reference count (use_count) minimum of 2 even though they should have 1!
Debugging into the boost’s code I found out that the ref. count is increased to value of 2 in “shared_ptr_helper.hpp”, line 181 where the pointer is stored inside map “m_o_sp”. This map however never gets deleted and hence the shared_ptr seems to be stored there for ever.
Please, am I doing something wrong or is there a bug? How to fix this?
The sample code I use is below. I would like to emphasise that I make sure the archives are deleted after being used. So it should release the resources.
I am using Mac OS X 10.10.2, boost version 1.57.0
Thank you for help,
Josef
****************
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/filtering_stream.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() {
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"));
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"));
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;
}
****************