I have now another problem, too. When I am loading a map that contains a shared_ptr I have memory leaks, because the use_count is wrong. In my real project I have a use_count of 5, but I can get it down to 2, when I am using new to create the object, that has the map. I've written a small example, that is basically the same as my real project, but it has also a use_count of 2 after loading the map in:
#include
#include #include
#include #include #include
#include <fstream>
struct ATest { int i; ATest() : i( 1 ) { } virtual ~ATest(){}
template< class Archive > void serialize( Archive& ar, const unsigned ) { ar & i; } }; BOOST_CLASS_EXPORT( ATest )
struct BTest : ATest { int c; BTest() : c( 2 ) { }
template< class Archive > void serialize( Archive& ar, const unsigned ) { ar & boost::serialization::base_object< ATest >( *this ); ar & c; }
}; BOOST_CLASS_EXPORT( BTest )
typedef std::map< int, boost::shared_ptr< ATest > > MyMap;
BOOST_CLASS_TRACKING( MyMap, boost::serialization::track_never)
int main() { MyMap myMap; myMap[ 0 ] = boost::shared_ptr< ATest >( new BTest ); myMap[ 1 ] = boost::shared_ptr< ATest >( new BTest );
std::ofstream ofs( "map.txt" ); boost::archive::text_oarchive oar( ofs );
oar << myMap;
ofs.close();
std::ifstream ifs( "map.txt" ); boost::archive::text_iarchive iar( ifs );
MyMap myMap1;
iar >> myMap1; }
Maybe you can tell me what I am doing wrong :-) Sascha Friedmann