On Fri, Jul 17, 2015 at 10:45 AM, Gordon Sim <g...@redhat.com> wrote:

> Still digesting the explanation (thanks!) but one follow up question:
>
> On 07/17/2015 04:37 PM, Rafael Schloming wrote:
>
>> it isn't actually possible to use the object when there refcount is 0.
>>
>
> What is the purpose of the incref/decref pattern then, e.g. as used in
> pn_session_free()? That is designed to trigger the finalizer, right? If so
> it would seem that can only happen if the reference count is 0 at the point
> when pn_session_free() is called.
>
> (And if it was valid to call pn_session_free for a given session, then
> that session would have been valid for use before that, no?)
>

The refcount should never be zero going into pn_session_free. The situation
where it triggers the finalizer is when the refcount is 1 upon entering
pn_session_free, but the referenced boolean is false meaning that the one
remaining reference that exists is the parent -> child pointer (in this
case the connection -> session pointer). The incref triggers the
pn_session_incref hook which flips the reference around so that the session
-> connection pointer is now the reference and the connection -> session
pointer is the borrowed reference. Logically this momentarily increases the
refcount from 1 to 2 and then the hook decreases it back to 1 and the
decref triggers the finalizer.

Now if pn_session_free is called when the refcount is > 1, or if
pn_ep_decref ends up posting events (events create references to endpoint
objects and thereby increase the refcount), then the pn_incref/decref ends
up being a noop and the session is simply marked as free but finalized when
the last reference to it is decref'ed.

--Rafael

Reply via email to