Boost logo

Boost Users :

From: Matthias Troyer (troyer_at_[hidden])
Date: 2008-06-26 02:58:02


The reason is that array deserialization does not allocate memory for
the array, but assumes that the memory has already been allocated. In
your case, you allocate memory in the constructor taking three
arguments, but not in the default constructor. Hence the segmentation
fault.

Matthias

On Jun 25, 2008, at 10:04 PM, michalurbanski wrote:

> Hi all,
>
> I have a problem deserializing an array. With the code (1) I get a
> Segmenetation Fault with stacktrace (2).
>
> Any help appriciated.
>
> Kind regards,
> MU.
>
>
> ------------------------------------------------------------
> Attachment (1):
>
> #include <fstream>
> #include <iostream>
> #include <boost/archive/xml_iarchive.hpp>
> #include <boost/archive/xml_oarchive.hpp>
> #include <boost/serialization/array.hpp>
> #include <boost/serialization/export.hpp>
>
> using namespace std;
>
> class Test {
> private:
> friend class boost::serialization::access;
>
> template<class Archive>
> void serialize(Archive & ar, const unsigned int version) {
> ar & BOOST_SERIALIZATION_NVP(a);
> ar & BOOST_SERIALIZATION_NVP(b);
> ar & boost::serialization::make_array<float>(c, 1);
> }
>
> int a;
> int b;
> float* c;
> public:
> inline Test() {
> c = 0;
> }
>
> inline Test(int a, int b, float c) {
> this->a = a;
> this->b = b;
> this->c = new float[1];
> this->c[0] = c;
> }
>
> inline void display() {
> cout << "a:" << a << endl;
> cout << "b:" << b << endl;
> cout << "*c:" << *c << endl;
> }
> };
>
> BOOST_CLASS_EXPORT(Test)
>
> int main() {
> // SERIALIZE:
> std::ofstream ofs("filename.xml");
>
> Test* t = new Test(1, 2, 3.3);
>
> boost::archive::xml_oarchive oa(ofs);
> oa << BOOST_SERIALIZATION_NVP(t);
>
> ofs.close();
>
> // DESERIALIZE:
> std::ifstream ifs("filename.xml");
> Test* t2;
>
> boost::archive::xml_iarchive ia(ifs);
> ia >> BOOST_SERIALIZATION_NVP(t2);
>
> t2->display();
>
> return 0;
> }
>
> ------------------------------------------------------------
> Attachment 2:
>
> 51 std::__convert_to_v<float>() 0xb7e6bc5f
> 50 std::num_get<char, std::istreambuf_iterator<char,
> std::char_traits<char>
>>> ::do_get() 0xb7e33c8a
> 49 std::istream::operator>>() 0xb7e1c9c7
> 48
> boost::archive::basic_text_iprimitive<std::istream>::load<float>()
> /usr/local/include/boost-1_35/boost/archive/
> basic_text_iprimitive.hpp:80
> 0x0804e687
> 47
> boost
> ::archive
> ::xml_iarchive_impl<boost::archive::xml_iarchive>::load<float>() /
> usr/local/include/boost-1_35/boost/archive/xml_iarchive.hpp:58
> 0x0804e6a9
> 46
> boost
> ::archive::load_access::load_primitive<boost::archive::xml_iarchive,
> float>()
> /usr/local/include/boost-1_35/boost/archive/detail/iserializer.hpp:96
> 0x0804e6c4
> 45
> boost
> ::archive::detail::load_non_pointer_type<boost::archive::xml_iarchive,
> float>::load_primitive::invoke()
> /usr/local/include/boost-1_35/boost/archive/detail/iserializer.hpp:339
> 0x0804e6de
> 44
> boost
> ::archive::detail::load_non_pointer_type<boost::archive::xml_iarchive,
> float>::invoke()
> /usr/local/include/boost-1_35/boost/archive/detail/iserializer.hpp:414
> 0x0804e6f8
> 43 boost::archive::load<boost::archive::xml_iarchive, float>()
> /usr/local/include/boost-1_35/boost/archive/detail/iserializer.hpp:561
> 0x0804e712
> 42
> boost
> ::archive
> ::detail
> ::common_iarchive
> <boost::archive::xml_iarchive>::load_override<float>()
> /usr/local/include/boost-1_35/boost/archive/detail/
> common_iarchive.hpp:59
> 0x0804e736
> 41
> boost
> ::archive
> ::basic_xml_iarchive
> <boost::archive::xml_iarchive>::load_override<float>()
> /usr/local/include/boost-1_35/boost/archive/basic_xml_iarchive.hpp:81
> 0x0804e77a
> 40
> boost
> ::archive
> ::xml_iarchive_impl
> <
> boost
> ::archive
> ::xml_iarchive>::load_override<boost::serialization::nvp<float>
> const>()
> /usr/local/include/boost-1_35/boost/archive/xml_iarchive.hpp:74
> 0x0804e7b6
> 39
> boost
> ::archive
> ::detail
> ::interface_iarchive
> <
> boost
> ::archive
> ::xml_iarchive>::operator>><boost::serialization::nvp<float> const>()
> /usr/local/include/boost-1_35/boost/archive/detail/
> interface_iarchive.hpp:63
> 0x0804e7e2
> 38
> boost
> ::archive
> ::detail
> ::interface_iarchive
> <
> boost
> ::archive::xml_iarchive>::operator&<boost::serialization::nvp<float>
> const>()
> /usr/local/include/boost-1_35/boost/archive/detail/
> interface_iarchive.hpp:70
> 0x0804e812
> 37
> boost
> ::serialization
> ::array<float>::serialize<boost::archive::xml_iarchive>() /usr/local/
> include/boost-1_35/boost/serialization/array.hpp:39
> 0x0804e86b
> 36
> boost::serialization::access::serialize<boost::archive::xml_iarchive,
> boost::serialization::array<float> >()
> /usr/local/include/boost-1_35/boost/serialization/access.hpp:109
> 0x0804e89b
> 35 boost::serialization::serialize<boost::archive::xml_iarchive,
> boost::serialization::array<float> >()
> /usr/local/include/boost-1_35/boost/serialization/serialization.hpp:81
> 0x0804e8bd
> 34 boost::serialization::serialize_adl<boost::archive::xml_iarchive,
> boost::serialization::array<float> >()
> /usr/local/include/boost-1_35/boost/serialization/serialization.hpp:
> 140
> 0x0804e8fb
> 33
> boost
> ::archive::detail::load_non_pointer_type<boost::archive::xml_iarchive,
> boost::serialization::array<float> >::load_only::invoke()
> /usr/local/include/boost-1_35/boost/archive/detail/iserializer.hpp:349
> 0x0804e91e
> 32
> boost
> ::archive::detail::load_non_pointer_type<boost::archive::xml_iarchive,
> boost::serialization::array<float> >::invoke()
> /usr/local/include/boost-1_35/boost/archive/detail/iserializer.hpp:414
> 0x0804e938
> 31 boost::archive::load<boost::archive::xml_iarchive,
> boost::serialization::array<float> >()
> /usr/local/include/boost-1_35/boost/archive/detail/iserializer.hpp:561
> 0x0804e952
> 30 boost::archive::load_wrapper<boost::archive::xml_iarchive,
> boost::serialization::array<float> >()
> /usr/local/include/boost-1_35/boost/archive/detail/iserializer.hpp:583
> 0x0804e96c
> 29 boost::archive::load<boost::archive::xml_iarchive,
> boost::serialization::array<float> >()
> /usr/local/include/boost-1_35/boost/archive/detail/iserializer.hpp:589
> 0x0804e98e
> 28
> boost
> ::archive
> ::detail
> ::common_iarchive
> <
> boost
> ::archive
> ::xml_iarchive>::load_override<boost::serialization::array<float>
> const>()
> /usr/local/include/boost-1_35/boost/archive/detail/
> common_iarchive.hpp:59
> 0x0804e9b2
> 27
> boost
> ::archive
> ::basic_xml_iarchive
> <
> boost
> ::archive
> ::xml_iarchive>::load_override<boost::serialization::array<float>
> const>()
> /usr/local/include/boost-1_35/boost/archive/basic_xml_iarchive.hpp:66
> 0x0804e9d4
> 26
> boost
> ::archive
> ::xml_iarchive_impl
> <
> boost
> ::archive
> ::xml_iarchive>::load_override<boost::serialization::array<float>
> const>()
> /usr/local/include/boost-1_35/boost/archive/xml_iarchive.hpp:74
> 0x0804e9f6
> 25
> boost
> ::archive
> ::detail
> ::interface_iarchive
> <
> boost
> ::archive
> ::xml_iarchive>::operator>><boost::serialization::array<float>
> const>()
> /usr/local/include/boost-1_35/boost/archive/detail/
> interface_iarchive.hpp:63
> 0x0804ea22
> 24
> boost
> ::archive
> ::detail
> ::interface_iarchive
> <
> boost
> ::archive
> ::xml_iarchive>::operator&<boost::serialization::array<float> const>()
> /usr/local/include/boost-1_35/boost/archive/detail/
> interface_iarchive.hpp:70
> 0x0804ea52
> 23 Test::serialize<boost::archive::xml_iarchive>()
> /home/wojtek/workspaceCDT/test_serial/src/main.cpp:18 0x0804eaf9
> 22
> boost::serialization::access::serialize<boost::archive::xml_iarchive,
> Test>() /usr/local/include/boost-1_35/boost/serialization/access.hpp:
> 109
> 0x0804eb1b
> 21 boost::serialization::serialize<boost::archive::xml_iarchive,
> Test>()
> /usr/local/include/boost-1_35/boost/serialization/serialization.hpp:81
> 0x0804eb3d
> 20 boost::serialization::serialize_adl<boost::archive::xml_iarchive,
> Test>()
> /usr/local/include/boost-1_35/boost/serialization/serialization.hpp:
> 140
> 0x0804eb7b
> 19 boost::archive::detail::iserializer<boost::archive::xml_iarchive,
> Test>::load_object_data()
> /usr/local/include/boost-1_35/boost/archive/detail/iserializer.hpp:160
> 0x0804ec28
> 18 boost::archive::detail::basic_iarchive::load_object() 0xb7ee30bc
> 17
> boost
> ::archive::detail::load_non_pointer_type<boost::archive::xml_iarchive,
> Test>::load_standard::invoke()
> /usr/local/include/boost-1_35/boost/archive/detail/iserializer.hpp:364
> 0x0804ed2b
> 16
> boost
> ::archive::detail::load_non_pointer_type<boost::archive::xml_iarchive,
> Test>::invoke()
> /usr/local/include/boost-1_35/boost/archive/detail/iserializer.hpp:414
> 0x0804ed46
> 15 boost::archive::load<boost::archive::xml_iarchive, Test>()
> /usr/local/include/boost-1_35/boost/archive/detail/iserializer.hpp:561
> 0x0804ed60
> 14
> boost
> ::archive
> ::detail
> ::common_iarchive<boost::archive::xml_iarchive>::load_override<Test>()
> /usr/local/include/boost-1_35/boost/archive/detail/
> common_iarchive.hpp:59
> 0x0804ed84
> 13
> boost
> ::archive
> ::basic_xml_iarchive
> <boost::archive::xml_iarchive>::load_override<Test>()
> /usr/local/include/boost-1_35/boost/archive/basic_xml_iarchive.hpp:81
> 0x0804edc8
> 12
> boost
> ::archive
> ::xml_iarchive_impl
> <
> boost
> ::archive
> ::xml_iarchive>::load_override<boost::serialization::nvp<Test>
> const>()
> /usr/local/include/boost-1_35/boost/archive/xml_iarchive.hpp:74
> 0x0804ee04
> 11
> boost
> ::archive
> ::detail
> ::interface_iarchive
> <
> boost
> ::archive::xml_iarchive>::operator>><boost::serialization::nvp<Test>
> const>()
> /usr/local/include/boost-1_35/boost/archive/detail/
> interface_iarchive.hpp:63
> 0x0804ee30
> 10
> boost
> ::archive::detail::pointer_iserializer<boost::archive::xml_iarchive,
> Test>::load_object_ptr()
> /usr/local/include/boost-1_35/boost/archive/detail/iserializer.hpp:303
> 0x0804ef27
> 9 boost::archive::detail::basic_iarchive_impl::load_pointer()
> 0xb7ee3fc4
> 8 boost::archive::detail::basic_iarchive::load_pointer() 0xb7ee3159
> 7
> boost
> ::archive::detail::load_pointer_type<boost::archive::xml_iarchive,
> Test*>::invoke()
> /usr/local/include/boost-1_35/boost/archive/detail/iserializer.hpp:479
> 0x0804ffa6
> 6 boost::archive::load<boost::archive::xml_iarchive, Test*>()
> /usr/local/include/boost-1_35/boost/archive/detail/iserializer.hpp:561
> 0x08050000
> 5
> boost
> ::archive
> ::detail
> ::common_iarchive
> <boost::archive::xml_iarchive>::load_override<Test*>()
> /usr/local/include/boost-1_35/boost/archive/detail/
> common_iarchive.hpp:59
> 0x08050024
> 4
> boost
> ::archive
> ::basic_xml_iarchive
> <boost::archive::xml_iarchive>::load_override<Test*>()
> /usr/local/include/boost-1_35/boost/archive/basic_xml_iarchive.hpp:81
> 0x08050068
> 3
> boost
> ::archive
> ::xml_iarchive_impl
> <
> boost
> ::archive
> ::xml_iarchive>::load_override<boost::serialization::nvp<Test*>
> const>()
> /usr/local/include/boost-1_35/boost/archive/xml_iarchive.hpp:74
> 0x080500a4
> 2
> boost
> ::archive
> ::detail
> ::interface_iarchive
> <
> boost
> ::archive
> ::xml_iarchive>::operator>><boost::serialization::nvp<Test*> const>()
> /usr/local/include/boost-1_35/boost/archive/detail/
> interface_iarchive.hpp:63
> 0x080500d0
> 1 main() /home/wojtek/workspaceCDT/test_serial/src/main.cpp:61
> 0x0804c542
> _______________________________________________
> 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