Boost logo

Boost :

Subject: Re: [boost] Subject: Re: [shared_ptr] Where is the bug?
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2008-10-03 12:39:22


----- Original Message -----
From: "Kasra Nassiri(Math & ComSci)" <kasra_n500_at_[hidden]>
To: <boost_at_[hidden]>
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


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