Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2002-01-14 12:33:44

----- Original Message -----
From: "Luigi Ballabio" <ballabio_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Monday, January 14, 2002 12:24 PM
Subject: Re: [boost] shared_ptr: unsafe, or am I crazy?

> At 08:56 AM 1/14/02 -0500, you wrote:
> >struct Base { Base(); ~Base(); };
> >struct Derived : Base { Derived(); ~Derived(); };
> >
> >void unsafe()
> >{
> > shared_ptr<Derived> d(new Derived);
> > shared_ptr<Base> b(d);
> > d.reset();
> > b.reset(); // equivalent to delete (Base*)(new Derived)!!
> >};
> >
> >Isn't shared_ptr just screaming for the application of John Maddock's new
> >is_polymorphic<> trait?
> Maybe I'm just about to embarass myself, but isn't Base just screaming for
> having a virtual destructor instead, if you want to use it this way?

The point is that non-polymorphic public bases are an important and useful
construct, and safe as long as you don't explicitly delete a derived through
the base pointer. However, since shared_ptr deletes "implicitly", allowing
implicit conversion of shared_ptr<Derived> to shared_ptr<Base> can be
problematic. The whole point of a shared_ptr<> is that you don't have to
worry about the destruction of the object - the library will handle it
correctly for you.


Boost list run by bdawes at, gregod at, cpdaniel at, john at