Boost logo

Boost Users :

From: David Klein (dave_chp_at_[hidden])
Date: 2007-05-02 07:34:48


Richard Damon wrote:
> One comment is that:
>
> Class A {
> }; // No virtual destructor
>
> Class B : public A {
> }
>
> shared_ptr<A>(new B)
>
> Should cause UB as the shared pointer will will eventually call delete on
> its parameter, and since A has no virtual destructor and is NOT the type the
> object was created with, you get UB.
>
> It basically boils down to the equivalent to this
>
> A* ptr = new B;
> delete ptr;
>
> which is a no-no.
>
> A NEEDS to have a virtual destructor to use it the way you are.
> Richard
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
>
Hi Richard,

actually it's ok with shared_ptr. Here is a short snip from the docs:

...
This constructor has been changed to a template in order to remember the
actual pointer type passed. The destructor will call delete with the
same pointer, complete with its original type, even when T does not have
a virtual destructor, or is void.
...


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