Boost logo

Boost :

Subject: Re: [boost] [exception] warning about non-virtual destructor - resolution?
From: Hite, Christopher (Christopher.Hite_at_[hidden])
Date: 2012-04-13 09:06:18


This is annoying me too:
boost_1_49_0/boost/checked_delete.hpp:34:5: warning: deleting object of polymorphic class type 'boost::error_info<boost::tag_original_exception_type, const std::type_info*>' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]

A) easy solution
/boost_1_49_0/boost/exception/detail/error_info_impl.hpp
It's probably easiest to just make ~error_info_base() virtual. It doesn't cost much since there's already a virtual table.

B) harder solution
The other cooler C++11 alternative is declare error_info final. I tried it on gcc-4.7.0 and it works wonderfully. Since the compiler knows you can not derive a class from that type it knows a that deconstructor is complete.

struct base{
        virtual void foo();
};

struct derived final : base { };

//struct illegal : derived {}; // error: cannot derive from 'final' base 'derived' in derived type 'illegal'

void kill(derived* d){
        delete d; // no complaint
}

I vote for A, because you'd anyway have to detect support for final.

Chris


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk