Boost logo

Boost Users :

Subject: Re: [Boost-users] shared_ptr memory leak 2
From: Peter Dimov (pdimov_at_[hidden])
Date: 2009-02-28 19:22:02


gast128:

> The case seems entered by Peter Dimov as bug entry:
>
> https://svn.boost.org/trac/boost/ticket/2813

Yes, I consider this a bug. This is the test I've come up with:

#include <boost/shared_ptr.hpp>
#include <boost/detail/lightweight_test.hpp>

class X
{
public:

    static int instances;

    X()
    {
        ++instances;
    }

    ~X()
    {
        --instances;
    }

private:

    X( X const& );
};

int X::instances = 0;

class Y
{
public:

    static int instances;

    Y()
    {
        ++instances;
    }

    ~Y()
    {
        --instances;
    }

private:

    Y( Y const& );
};

int Y::instances = 0;

static boost::shared_ptr<void> s_pv;

class Z
{
public:

    static int instances;

    Z()
    {
        ++instances;
    }

    ~Z()
    {
        --instances;

        boost::shared_ptr<void> pv( new Y );
        s_pv = pv;
    }

private:

    Z( Z const& );
};

int Z::instances = 0;

int main()
{
    BOOST_TEST( X::instances == 0 );
    BOOST_TEST( Y::instances == 0 );
    BOOST_TEST( Z::instances == 0 );

    {
        boost::shared_ptr<void> pv( new Z );
        s_pv = pv;
    }

    BOOST_TEST( X::instances == 0 );
    BOOST_TEST( Y::instances == 0 );
    BOOST_TEST( Z::instances == 1 );

    {
        boost::shared_ptr<void> pv( new X );
        s_pv = pv;
    }

    BOOST_TEST( X::instances == 0 );
    BOOST_TEST( Y::instances == 1 );
    BOOST_TEST( Z::instances == 0 );

    s_pv.reset();

    BOOST_TEST( X::instances == 0 );
    BOOST_TEST( Y::instances == 0 );
    BOOST_TEST( Z::instances == 0 );

    return boost::report_errors();
}


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