
----- Original Message ----- From: "Kasra Nassiri(Math & ComSci)" <kasra_n500@yahoo.com> To: <boost@lists.boost.org> Sent: Friday, October 03, 2008 11:25 AM Subject: [boost] Subject: Re: [shared_ptr] Where is the bug?
Hi,
Well the bug is in the fact the when you allocated: B* b = new X();
You have allocated an int extra, thus when you call: delete b;
You are only destructing what was allocated by B, which cause a sizeof(int) bytes memory leak each time you do this.
Just use a virtual destructor on virtual ~B() ... and that would take care of it all.
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. I have added the delete of B* to show the difference. So if you want the question is, is there someting wrong on the program. 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; return 0; } Vicente