Boost logo

Boost Users :

Subject: Re: [Boost-users] [Serialization] Error in serialization of a hash map
From: Robert Ramey (ramey_at_[hidden])
Date: 2009-02-23 14:53:04


I don't think that serialization of maps which containers is reliable.

The problem has to do with the fact that data is loaded then moved
and the "reset object address" function can't keep track of it.

For a truely reliable method, I would recommend putting your map into
some other container before serializing it and building the map after
loading it.

Robert Ramey
  "Jack Mullen" <jack_at_[hidden]> wrote in message news:5283CCA479EE4902A7D91F14EDDFBA20_at_murgatroid...
  Hi,

  I'm having a bit of trouble while using the serialization library. (Boost v1.35, gcc, Red Hat Linux).
  I have a class containing a hash_map (key=string, value=shared_ptr to a class containing a deque):

  HashMapClass{
        friend class boost::serialization::access;
        template <class Archive>
        void serialize(Archive &ar, const unsigned in t version){
              ar & map;
        }

        __gun_cxx::hash_map<std::string const, boost::shared_ptr<DequeClass> map;
  }

  //create instance
  HashMapClass hashMap;

  // fill hashmap
  ...

  //perform serialization every hour or something
  std::ofstream ofs("file.txt");
  boost::archive::binary_oarchive oa(ofs);
  const HashMapClass &map = hashMap;
  oa << map;

  The Problem:
  This works fine for several saves. I can see the file created changing each time the serialization is done. Then after about four or five saves, it seg faults at the place described below. It looks to me like it's having problems iterating over the hash_map.

  Any advice on how to diagnose or fix this would be appreciated,

  Many thanks

  Jack

  Segfault at:

  (__gnu_cxx::_Hashtable_const_iterator<std::pair<std::string const, boost::shared_ptr<HashMapClass> >, std::string const, __gnu_cxx::hash<std::string const>, std::_Select1st<std::pair<std string const, boost::shared_ptr<HashMapClass> > >, std::equal_to<std::string const>, std::allocator<boost::shared_ptr<HashMapClass> > >::operator++()+0xe)[0x65a825a]

  (boost::archive::detail::oserializer<boost::archive::binary_oarchive, __gnu_cxx::hash_map<std::string const, boost::shared_ptr<HashMapClass>, __gnu_cxx::hash<std::string const>, std::equal_to<std::string const>, std::allocator<boost::shared_ptr<HashMapClass> > >::save_object_data(boost::archive::detail::basic_oarchive&, void const*) const+0x1b2)[0x65a90da]

  (/usr/lib/libboost_serialization-gcc34-mt-1_35.so.1.35.0(boost::archive::detail::basic_oarchive_impl::save_object(boost::archive::detail::basic_oarchive&, void const*, boost::archive::detail::basic_oserializer const&)+0xd1)[0x8497cd]

  (/usr/lib/libboost_serialization-gcc34-mt-1_35.so.1.35.0(boost::archive::detail::basic_oarchive::save_object(void const*, boost::archive::detail::basic_oserializer const&)+0x23)[0x849213]

  (boost::archive::detail::oserializer<boost::archive::binary_oarchive, HashMapClass>::save_object_data(boost::archive::detail::basic_oarchive&, void const*) const+0x15c)[0x65a7db4]

  (/usr/lib/libboost_serialization-gcc34-mt-1_35.so.1.35.0(boost::archive::detail::basic_oarchive_impl::save_object(boost::archive::detail::basic_oarchive&, void const*, boost::archive::detail::basic_oserializer const&)+0xd1)[0x8497cd]

  (/usr/lib/libboost_serialization-gcc34-mt-1_35.so.1.35.0(boost::archive::detail::basic_oarchive::save_object(void const*, boost::archive::detail::basic_oserializer const&)+0x23)[0x849213]

------------------------------------------------------------------------------

  _______________________________________________
  Boost-users mailing list
  Boost-users_at_[hidden]
  http://lists.boost.org/mailman/listinfo.cgi/boost-users



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