Boost logo

Boost :

From: Jeff Flinn (TriumphSprint2000_at_[hidden])
Date: 2004-05-11 13:43:07


Ok, I've determined that the memory leak I was seeing is real, atleast in
vc7.1. The boost::detail::sp_counted_base_impl<P,D> held by a serialization
loaded shared_ptr never gets deleted. This is due to
boost::serialization::load_construct_data setting weak_count to 0. When the
sp_counted_base::weak_release() gets called new_weak_count is -1 so destruct
is not called.

A fix that works for my application, is to comment out the line:

    shared_ptr_access::weak_count(*t) = 0;

in

    template<class Archive, class P, class D>
    inline void load_construct_data(
    Archive & ar,
    boost::detail::sp_counted_base_impl<P, D> * t,
    const unsigned int /* file_version */

The following was used to reproduce the problem in vc7.1.

// SerializationSharedPtrTest.cpp : Defines the entry point for the console
application.
//

#define CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

struct Checker
{
   Checker()
   { _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF
                   | _CRTDBG_CHECK_ALWAYS_DF
                   | _CRTDBG_LEAK_CHECK_DF );
   }
};

Checker lChecker;

#include <boost/serialization/shared_ptr.hpp>

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

#include <fstream>

typedef std::string tName;

class PointedToClass
{
   int mInt;

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

public:
   ~PointedToClass(){}
    PointedToClass():mInt(123){}
    PointedToClass( int aInt ):mInt( aInt){}
    PointedToClass( const PointedToClass& aRhs ):mInt(aRhs.mInt){}
};

typedef boost::shared_ptr< PointedToClass > tPtr;

int main( int argc, char* argv[] )
{
   {
      std::ofstream lOut( "SomeClass.archive", std::ios_base::out |
std::ios_base::trunc );

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

         tPtr lPtr( new PointedToClass(456) );

         oa << boost::serialization::make_nvp( "Ptr" , lPtr );
      }
   }
   {
    std::ifstream lIn( "SomeClass.archive" );

      tPtr lPtr;

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

         ia >> boost::serialization::make_nvp( "Ptr" , lPtr );
    }
   }

   return 0;
}


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