Boost logo

Boost Users :

Subject: Re: [Boost-users] [signals2] tracking intrusively ref-counted object
From: Frank Mori Hess (fmhess_at_[hidden])
Date: 2009-09-06 09:52:52


On Sunday 06 September 2009, Igor R wrote:
> Actually, you mean I have to override object's release() method -- as
> this's the only piece of code that knows when ref.count drops to 1.
> I'm still not sure I understand how to make this solution thread-safe.
> Lets consider the following scheme:
>
> struct object
> {
> void init()
> {
> selfAncor_ = some_intrusive_ptr<object>(this);
> connection1_ = signal1_.connect(&handle, ref(selfAncor_));
> connection2_ = signal2_.connect(&handle, ref(selfAncor_));

No, you want to bind a copy of the smart pointer to the slot by value.
Making the slot accept it by reference just avoids creating another
temporary copy during invocation, which makes the reference counting more
complicated.

> }
>
> int release()
> {
> do_release();
> if (refcount_ == 1)
> {
> connection1_.disconnect();
> connection2_.disconnect();
> // the following line is unsafe, because some slot invocation
> might be in progress!
> selfAncor_.reset();

No, there's still a copy bound to the slot which won't be destroyed until
the invocation is finished and the signal garbage collects the
disconnected slot from its slot list.

> }
> }




Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net