Serialization of boost::shared_ptr - wrong ref. count?
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
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
Hi Robert, Thank you for checking it. I tried your modification and it does the same thing on my computer. Still use count two. It is really very strange. If you say that you are running the latest version of the serialization library, do you mean the one from boost 1.57 or already 1.58? Cheers, Josef
On 14 Apr 2015, at 06:21, Robert Ramey
wrote: 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
//#include #include <fstream> #include "test_tools.hpp" #include #include #include #include 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-r... Sent from the Boost - Users mailing list archive at Nabble.com. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
I tried to switch back to boost 1.56.0 and there it works!
On 14 Apr 2015, at 06:21, Robert Ramey
wrote: 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
//#include #include <fstream> #include "test_tools.hpp" #include #include #include #include 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-r... Sent from the Boost - Users mailing list archive at Nabble.com. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Josef Uher
-
Robert Ramey