[shared_ptr] Where is the bug?

Hi, I have the following program with the following output #include <iostream> #include <boost/shared_ptr.hpp> using namespace boost; struct B { ~B() { std::cout << "~B" << std::endl; }; }; struct X : B { ~X() { std::cout << "~X" << std::endl; }; int i; }; int main () { shared_ptr<B>* ptr=0; { shared_ptr<X> ptrX1(new X()); ptr = new shared_ptr<B>(ptrX1); } std::cout << "BEFORE" << std::endl; delete ptr; std::cout << "AFTER" << std::endl; B* b = new X(); std::cout << "BEFORE" << std::endl; delete b; std::cout << "AFTER" << std::endl; return 0; } BEFORE ~X ~B AFTER BEFORE ~B AFTER EXIT STATUS: 0 Where is the bug? Vicente P.S. I'm using g++ (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)

On Fri, Oct 3, 2008 at 2:26 AM, vicente.botet <vicente.botet@wanadoo.fr> wrote:
EXIT STATUS: 0 Where is the bug?
If you're wondering why X's destructor isn't run, you need to make B's destructor virtual: http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7 -- Matthew L. Creech

----- Original Message ----- From: "Matthew L. Creech" <mlcreech@gmail.com> To: <boost@lists.boost.org> Sent: Friday, October 03, 2008 8:30 AM Subject: Re: [boost] [shared_ptr] Where is the bug?
On Fri, Oct 3, 2008 at 2:26 AM, vicente.botet <vicente.botet@wanadoo.fr> wrote:
EXIT STATUS: 0 Where is the bug?
If you're wondering why X's destructor isn't run, you need to make B's destructor virtual:
Yes, I know. I was not wondering about why the X's destructor is not run when deleting b, but why the X's destructor is called when deleting ptr. int main () { shared_ptr<B>* ptr=0; { shared_ptr<X> ptrX1(new X()); ptr = new shared_ptr<B>(ptrX1); } std::cout << "BEFORE" << std::endl; delete ptr; std::cout << "AFTER" << std::endl; B* b = new X(); std::cout << "BEFORE" << std::endl; delete b; std::cout << "AFTER" << std::endl; return 0; }

On Fri, Oct 3, 2008 at 9:08 AM, vicente.botet <vicente.botet@wanadoo.fr> wrote:
Yes, I know. I was not wondering about why the X's destructor is not run when deleting b, but why the X's destructor is called when deleting ptr.
Because shared_ptr captures the destructor at the time it takes ownership of the object. If this happens when the object is being created (as in your example) the correct destructor is called even if it isn't virtual. It is common for base types that are designed to be managed by shared_ptr to use protected, non-virtual destructor (watch out for retarded gcc warnings.) Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode

----- Original Message ----- From: "Emil Dotchevski" <emil@revergestudios.com> To: <boost@lists.boost.org> Sent: Friday, October 03, 2008 8:32 PM Subject: Re: [boost] [shared_ptr] Where is the bug?
On Fri, Oct 3, 2008 at 9:08 AM, vicente.botet <vicente.botet@wanadoo.fr> wrote:
Yes, I know. I was not wondering about why the X's destructor is not run when deleting b, but why the X's destructor is called when deleting ptr.
Because shared_ptr captures the destructor at the time it takes ownership of the object. If this happens when the object is being created (as in your example) the correct destructor is called even if it isn't virtual.
It is common for base types that are designed to be managed by shared_ptr to use protected, non-virtual destructor (watch out for retarded gcc warnings.)
Thanks for your post, What is the advantage to use protected destructor when used with shared pointers? Shouldn't you have a deleter functor? Vicente

On Fri, Oct 3, 2008 at 1:11 PM, vicente.botet <vicente.botet@wanadoo.fr> wrote:
----- Original Message ----- From: "Emil Dotchevski"
On Fri, Oct 3, 2008 at 9:08 AM, vicente.botet <vicente.botet@wanadoo.fr> wrote:
Yes, I know. I was not wondering about why the X's destructor is not run when deleting b, but why the X's destructor is called when deleting ptr.
Because shared_ptr captures the destructor at the time it takes ownership of the object. If this happens when the object is being created (as in your example) the correct destructor is called even if it isn't virtual.
It is common for base types that are designed to be managed by shared_ptr to use protected, non-virtual destructor (watch out for retarded gcc warnings.)
What is the advantage to use protected destructor when used with shared pointers?
As compared to a virtual destructor, calling a non-virtual destructor is faster and could be inlined. Making a destructor protected means that only the derived class destructor is able to call it. Non-virtual destructor of a base type shouldn't be public because calling it may lead to undefined behavior. Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
participants (4)
-
Emil Dotchevski
-
Matthew L. Creech
-
Peter Bartlett
-
vicente.botet