Boost logo

Boost Users :

Subject: [Boost-users] Strange issue with Serialization of BiMaps
From: Mangal (mangalmanish_at_[hidden])
Date: 2010-01-01 01:00:37


Hi
I am using Boost 1.40.0 and stlport 5.0 on Visual Studio 2008.
The Bimap deserialization succeeds for 24 elements but starts failing after
that.

Here is the file with all the code

#include "stdafx.h"
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/functional/hash.hpp>
#include <boost/bimap/bimap.hpp>
#include <fstream>
#include <exception>
#include <iostream>

using namespace boost;
using namespace boost::bimaps;

typedef unsigned int uint;

struct GeoNode
{
public:
        GeoNode(){}
        GeoNode(uint x, uint y ): x_geocode(x), y_geocode(y) {}

        friend bool operator==(GeoNode const& a, GeoNode const& b)
    {
        return a.x_geocode == b.x_geocode && a.y_geocode == b.y_geocode;
    }

        friend std::size_t hash_value(GeoNode const& g)
    {
        std::size_t seed = 0;
        boost::hash_combine(seed, g.x_geocode);
        boost::hash_combine(seed, g.y_geocode);

        return seed;
    }

        friend bool operator < (GeoNode const& a, GeoNode const& b)
        {
                if (a.x_geocode != b.x_geocode)
                        return a.x_geocode < b.x_geocode;

                return a.y_geocode < b.y_geocode;
        }

        friend std::ostream& operator << (std::ostream& os, const GeoNode& g)
        {
                os<<"X: "<<g.x_geocode<<" Y: "<<g.y_geocode<<std::endl;
                return os;
        }

private:

        friend class boost::serialization::access;
           
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & x_geocode;
        ar & y_geocode;
    }

public:

        uint x_geocode;
        uint y_geocode;
};

template< class MapType >
void print_map(const MapType & m)
{
    typedef typename MapType::const_iterator const_iterator;
    for( const_iterator iter = m.begin(), iend = m.end(); iter != iend;
++iter )
    {
        std::cout << iter->first << "-->" << iter->second << std::endl;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
        typedef bimap<uint, GeoNode> Store_type;
        const char* fileName = "Nodes.dat";
        Store_type storeToWrite;
        Store_type storeThatsRead;

        int countThatWorks = 24;
        int countThatFails = 25;
        //insert Data
        for ( int i =0;i<countThatFails;++i)
        {
                storeToWrite.insert(Store_type::value_type(i, GeoNode(i+1, i+2)));
        }

        try
        {
                //write to file
                {
                        std::ofstream ofs(fileName);
                        boost::archive::binary_oarchive oa(ofs);
                        oa << const_cast<const Store_type&> (storeToWrite);
                }
                //read from File
                {
                        std::ifstream ifs(fileName);
                        boost::archive::binary_iarchive ia(ifs);
                        ia >> storeThatsRead;
                }

                print_map(storeThatsRead.left);
        }
        catch (std::exception e)
        {
                std::cout<<e.what();
        }
        return 0;
}

An unknown exception is thrown when more than 24 elements are added. Tried
to debug but right now can't make much sense.

Any help is appreciated

Thanks

-- 
View this message in context: http://old.nabble.com/Strange-issue-with-Serialization-of-BiMaps-tp26983930p26983930.html
Sent from the Boost - Users mailing list archive at Nabble.com.

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