Boost logo

Boost :

From: Jeff Flinn (TriumphSprint2000_at_[hidden])
Date: 2004-04-23 13:36:53


The following code produces and access violation when loading the NULL
shared_ptr. Is this an issue of not having registered the type pointed to?
What would I need to do to appropriately handle this situation?

#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/string.hpp>

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>

#include <fstream>

typedef std::string tName;

class PointedToClass
{
   tName mName;

   friend class boost::serialization::access;
   template< class Archive > void serialize( Archive &ar, unsigned int
version )
   {
      ar & boost::serialization::make_nvp( "Name", mName );
   }

public:
   ~PointedToClass(){}
    PointedToClass():mName("def"){}
};

typedef boost::shared_ptr<PointedToClass> tPtr;

class SomeClass
{
   tName mName;
   tPtr mPtr;

   friend class boost::serialization::access;
   template< class Archive > void serialize( Archive &ar, unsigned int
version )
   {
      ar & boost::serialization::make_nvp( "Name", mName );
      ar & boost::serialization::make_nvp( "Ptr", mPtr );
   }

public:

   SomeClass():mName("abc"),mPtr(){}
};

int main( int argc, char* argv[] )
{
   SomeClass lSomeClass;

   {
      std::ofstream lOut( "SomeClass.archive", std::ios_base::out |
std::ios_base::trunc );

      if( lOut.good() )
      {
         boost::archive::xml_oarchive oa( lOut );

         oa & boost::serialization::make_nvp( "SomeClass", lSomeClass );
      }
   }
   {
    std::ifstream lIn( "SomeClass.archive" );

    if( lIn.good() )
    {
         boost::archive::xml_iarchive ia( lIn );

         ia & boost::serialization::make_nvp( "SomeClass", lSomeClass );
    }
   }
   return 0;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk