Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-01-07 19:42:13


From: "Larry Evans" <jcampbell3_at_[hidden]>
> > Currently I use a conservative scan that assumes that shared_ptr
instances
> > have a layout of
> >
> > T * px;
> > counted_base * pi;
> > int id;
> >
> > with pointers being aligned on a DWORD boundary. 'pi' must be in the
count
> > map, and 'id' must be detail::shared_count::id. (It's possible to test
'px'
> > for validity too.) False positives should be extremely rare, but could
not
> > be ruled out entirely, and hence, breaking cycles may potentially
corrupt
> > the object.
> Would false positives be any less frequent than in the BW conservative
collector?

BW = Boehm-Weiser? AFAIK BW false positives happen when a memory location
resembles a valid heap address, and are much more likely.

> Why not use the Delef approach as demonstrated in shared_cyclic_ptr to
avoid
> false positives altogether?

I'm not familiar with Detlef's approach... does it require programmer
support?

> > Plain pointers would have to be followed by a real collector, but why
should
> > a "simple cycle-breaker" bother?
> >
> Because the following would require it in order to find the cycle:
>
> struc Y;
> struct X
> {
> Y* y; //cyclic involves this arc.
> };
> struct Y
> {
> boost::shared_ptr<X> x;
> };
>
> int main()
> {
> boost::shared_ptr<X> p1(new X);
> boost::shared_ptr<X> p2(new X);
>
> p1->y.x = p2;
> p2->y.x = p1;
>
> }

This doesn't look correct to me... did you mean something like

struct X
{
    Y * p;

    explicit X(Y * p): p(p) {}
    ~X() { delete p; }
};

struct Y
{
    shared_ptr<X> p;
};

int main()
{
    Y * py = new Y;
    shared_ptr<X> px(new X(py));
    py->p = px;
    px.reset();
}

?


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