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. With best regards Kasra Nassiri

----- 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

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Friday 03 October 2008 12:39 pm, vicente.botet 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. I
shared_ptr has some magic so it deletes through the same type of pointer that was passed to its constructor, which can eliminate the need for virtual destructors in some cases. Notice how the shared_ptr constructors take the pointer as a template parameter of the constructor, as opposed to just using the template type of the shared_ptr itself. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFI5lwz5vihyNWuA4URAmXBAKDY5j2HiOUUiZLOiftCi/hNbbATGQCfcID4 BUrHpCNhtk1mB3ONgVxYHx4= =HP9H -----END PGP SIGNATURE-----

----- Original Message ----- From: "Frank Mori Hess" <frank.hess@nist.gov> To: <boost@lists.boost.org> Sent: Friday, October 03, 2008 7:53 PM Subject: Re: [boost] Subject: Re: [shared_ptr] Where is the bug?
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On Friday 03 October 2008 12:39 pm, vicente.botet 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. I
shared_ptr has some magic so it deletes through the same type of pointer that was passed to its constructor, which can eliminate the need for virtual destructors in some cases. Notice how the shared_ptr constructors take the pointer as a template parameter of the constructor, as opposed to just using the template type of the shared_ptr itself.
Yeah! Thanks Frank. The magic is on the default deleter function, which knows the type and so use delete on the correct type. shared_ptr is really much more smarter than a raw pointer! Vicente
participants (4)
-
Frank Mori Hess
-
Kasra Nassiri(Math & ComSci)
-
Mathias Gaunard
-
vicente.botet