Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::exception_detail::error_info_base does not have virtual destructor
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2011-03-18 11:26:39


AMDG

On 03/18/2011 05:37 AM, Ted Byers wrote:
>> From: boost-users-bounces_at_[hidden]
> [mailto:boost-users-bounces_at_[hidden]] On Behalf Of Emil Dotchevski
>> Wouldn't a protected and non-virtual destructor be more appropriate in that
> case?
>
> How could a protected, non-virtual destructor be appropriate here at all?
>
> You have a single container of smart pointers to a base class. But the
> actually instances there represent possibly hundreds of different derived
> classes, generally with very different storage requirements. These are all
> objects on the heap, so all were created initially using operator new.
> Operator delete has thus to be called on each of them, and if the destructor
> is not virtual, you'd have to cast back to the real UDT in order to delete
> them. Since they are pointers to the base class, you'd have to try a
> dynamic cast each to every known UDT in order to get a pointer to the right
> class in order to properly delete it.

Nope. shared_ptr remembers the actual
type it was constructed with. It doesn't
need a virtual destructor, unless you cast
to the base before you create the shared_ptr.

> That would be an unmaintainable
> nightmare that is completely avoidable simply by using a virtual destructor
> and the combination of pointers to the base class living in shared pointers
> living in turn in a std::vector. The number of UDTs is huge and continually
> growing (for the biota portion of the model, there are typically UDTs for
> genera and species, and since there are so many species on the planet, I
> will probably not live long enough to see UDTs made for all of them).

In Christ,
Steven Watanabe


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