Opened 17 years ago

Closed 16 years ago

#593 closed Bugs (Invalid)

shared_ptr<> causes memory leacks and crash

Reported by: vlazarenko Owned by: Peter Dimov
Milestone: Component: smart_ptr
Version: None Severity:
Keywords: Cc:

Description

I have written this small test to reproduce the bug.
Please see..

#include <cstddef>
#include <boost/shared_ptr.hpp>
#include <boost/test/unit_test.hpp>

using namespace std;
using namespace boost;
using namespace boost::unit_test_framework;

  class A;

  class B
  {
  public:
    B(boost::shared_ptr<A> ptr) : ptr_(ptr) {}
    ~B() {}

  private:
    boost::shared_ptr<A> ptr_;
  };

  class A
  {
  public:
    A(int value) : value_(value) {}
    ~A() {}


  private:
    int value_;
  };

void test_shared_ptr()
{
  BOOST_CHECKPOINT("Testing shared pointer...");

  shared_ptr<A> a1(new A(10));

  {
    shared_ptr<B> b1 = a1->create();
  }

  {
    shared_ptr<B> b1 = a1->create();
  }
}

Change History (3)

comment:1 by Peter Dimov, 17 years ago

Logged In: YES 
user_id=305912

Your test doesn't compile since A doesn't define a 'create'
member function; if I fix it to use new B(a1) instead, it
doesn't link:

unresolved external symbol "class
boost::unit_test::test_suite * __cdecl
init_unit_test_suite(int,char * * const)"

What is your platform? What is your compiler? Does the
problem occur with the CVS version? Is this report by Howard
Hinnant:

http://lists.boost.org/Archives/boost/2006/03/102122.php

relevant?

comment:2 by vlazarenko, 17 years ago

Logged In: YES 
user_id=1024198

Sorry, I forgot to add "create" method. It is something like
this:

shared_ptr<B> A::create()
{
  return shared_ptr<B>(new B(shared_ptr<A>(this)));
}

I use Windows XP SP2, MS Visual Studio 2003 and boost
downloaded from SF.net version 1.33.1.

Actually, I am a little bit sorry, because it isn't a bug,
maybe issue. Code compiles OK, but works only with using
"boost::enable_shared_from_this<A>" inheritance of A class.

It was hard to find in documentation a little and may
confuse developers (like me :)) Maybe it would be good to
write it in documentation using red color.

Thanks

comment:3 by Peter Dimov, 16 years ago

Status: assignedclosed
Note: See TracTickets for help on using tickets.