Quote:Original post by Sneftel
Er, you should probably read the rest of the thread. shared_ptr's solution to the problem of incomplete types has the side effect of removing the need for virtual destructors, as long as the initial shared_ptr is constructed correctly.
Yes, and that's all good and well if you happen to be using shared_ptr, but try using intrusive_ptr without virtual d'tors and watch your application leak stuff all over the place.
Below is SiS-Shadowman's example modified for use with boost::intrusive_ptr ...
#include <iostream>#include <boost/intrusive_ptr.hpp>struct Data{ ~Data() { std::cout << "I got destroyed" << std::endl; }};struct A{ void ref() { ++count; } void unref() { --count; if ( count == 0 ) { delete this; } } size_t count; A() : count( 0 ) {}};void intrusive_ptr_add_ref( A* a ){ a->ref();}void intrusive_ptr_release( A* a ){ a->unref();}struct B : public A{ Data d;};int main( int argc, const char* argv[] ){ boost::intrusive_ptr< A > ptr( new B );}
Try it out yourself.
P.S. I realize that there are few problems that really require use of intrusive_ptr, but in general I think it's better to be safe than sorry. I mean if your class has any virtual functions, it'll have a vtable anyway so you may just as well go the extra mile and give it a virtual d'tor, too.