|
Boost Users : |
From: Sascha Friedmann (sascha.friedmann_at_[hidden])
Date: 2006-08-09 19:16:17
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 <boost/archive/text_oarchive.hpp>
> #include <boost/archive/text_iarchive.hpp>
>
> #include <boost/serialization/serialization.hpp>
> #include <boost/serialization/map.hpp>
> #include <boost/serialization/shared_ptr.hpp>
>
> #include <boost/serialization/export.hpp>
>
> #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
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