Skip to content

Memory leak during failed xml deserialization #260

Description

@hanzotutu

I have a project using boost::serialization (version 1.74) to and from xmls. Recently, we removed a data member inside a serializable class. When we try to read the old xml (the removed field is still in xml), the deserialization failed as expected. However, when we run Valgrind through the failed deserialization, we found some memory leak. I searched the internet for solution, and tried to catch the exception and do 'delete_created_pointer()', etc. However, nothing worked. I really need your help. Following are some details.

From xml, we read a vector of objects, and store them in shared_ptr of the common base class (serialization is exactly opposite):

     void SerializationFactory::register_in(boost::archive::xml_iarchive &ar,
          std::vector<std::shared_ptr<BaseObject> >& objs) {
          try {
              // ...
              ar & boost::serialization::make_nvp("My_ObjectDB", objs);
              // ...
          } catch (std::exception &) {
              ar.delete_created_pointers();
              objs.clear();
              throw;
          }
      }

The Valgrind report is like below:

...
==29114== 1,266 (1,080 direct, 186 indirect) bytes in 3 blocks are definitely lost in loss record 5,765 of 5,961
==29114==    at 0x4C31556: operator new(unsigned long) (vg_replace_malloc.c:344)
==29114==    by 0x1120AC1E: boost::archive::detail::basic_iarchive_impl::load_pointer(boost::archive::detail::basic_iarchive&, void*&, boost::archive::detail::basic_pointer_iserializer const*, boost::archive::detail::basic_pointer_iserializer const* (*)(boost::serialization::extended_type_info const&)) (in /home/appadmin/qrlib/alib/code/Release/Linux/lib/libALib.so)
==29114==    by 0x105730BB: boost::archive::detail::iserializer<boost::archive::xml_iarchive, std::shared_ptr<BaseObject> >::load_object_data(boost::archive::detail::basic_iarchive&, void*, unsigned int) const (in /home/appadmin/qrlib/alib/code/Release/Linux/lib/libALib.so)
==29114==    by 0x1120A4CA: boost::archive::detail::basic_iarchive::load_object(void*, boost::archive::detail::basic_iserializer const&) (in /home/appadmin/qrlib/alib/code/Release/Linux/lib/libALib.so)
==29114==    by 0x10570CB0: boost::archive::detail::iserializer<boost::archive::xml_iarchive, std::vector<std::shared_ptr<BaseObject>, std::allocator<std::shared_ptr<BaseObject> > > >::load_object_data(boost::archive::detail::basic_iarchive&, void*, unsigned int) const (in /home/appadmin/qrlib/alib/code/Release/Linux/lib/libALib.so)
==29114==    by 0x1120A4CA: boost::archive::detail::basic_iarchive::load_object(void*, boost::archive::detail::basic_iserializer const&) (in /home/appadmin/qrlib/alib/code/Release/Linux/lib/libALib.so)
==29114==    by 0x1056D13E: Addin::SerializationFactory::register_in(boost::archive::xml_iarchive&, std::vector<std::shared_ptr<BaseObject>, std::allocator<std::shared_ptr<BaseObject> > >&) (in /home/appadmin/qrlib/alib/code/Release/Linux/lib/libALib.so)
...

Thanks in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions