Subject: [Boost-bugs] [Boost C++ Libraries] #1942: Pointers to elements in a std::map are not serilaized correctly
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2008-05-23 06:12:26
#1942: Pointers to elements in a std::map are not serilaized correctly
------------------------------------+---------------------------------------
Reporter: bernhard.maeder_at_[hidden] | Owner: ramey
Type: Bugs | Status: new
Milestone: To Be Determined | Component: serialization
Version: Boost 1.35.0 | Severity: Problem
Keywords: |
------------------------------------+---------------------------------------
When serializing a std::map and some pointers that are referencing onto
elements of that map, those pointers are not loaded correctly.
Here's the code:
{{{
#include <sstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/map.hpp>
struct dummy
{
template<typename Archive>
void serialize(Archive & ar, const unsigned int version)
{
}
};
struct map_ref_test
{
std::map<std::size_t, dummy> m;
dummy * ref;
template<typename Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & m;
ar & ref;
}
};
int main()
{
std::ostringstream os;
map_ref_test m;
m.m[0] = dummy();
m.m[1] = dummy();
m.m[2] = dummy();
m.ref = &(m.m[1]);
// serialize
boost::archive::text_oarchive out(os);
out << const_cast<map_ref_test const &>(m);
// De-serialize
map_ref_test m2;
std::string ser = os.str();
std::istringstream is(ser);
boost::archive::text_iarchive in(is);
in >> m2;
std::cout << "0: " << &m.m[0] << std::endl;
std::cout << "1: " << &m.m[1] << std::endl;
std::cout << "2: " << &m.m[2] << std::endl;
std::cout << "ref: " << m.ref << std::endl << std::endl;
std::cout << "0: " << &m2.m[0] << std::endl;
std::cout << "1: " << &m2.m[1] << std::endl;
std::cout << "2: " << &m2.m[2] << std::endl;
std::cout << "ref: " << m2.ref << std::endl;
}
}}}
This creates the following output:
{{{
0: 0x805bfc4
1: 0x805bfe4
2: 0x805c004
ref: 0x805bfe4
0: 0x805ca04
1: 0x805ca24
2: 0x805ca44
ref: 0xbfacad5c
}}}
I'd expect the last line to read:
{{{
ref: 0x805ca24
}}}
The code works for std::vectors, so I'd assume it should work for maps
also.
--
Ticket URL: <http://svn.boost.org/trac/boost/ticket/1942>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.
This archive was generated by hypermail 2.1.7 : 2017-02-16 18:49:57 UTC