On 07/17/2015 08:11 PM, Rafael Schloming wrote:
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.
Ah, I see, I'd missed that hook. So the pn_incref(session) call actually
increments the reference of the *connection*, not the session in this
case. The pn_decref(session) then reduces the refcount of the session
from 1 to 0 which triggers the finalizer.
Thanks! I'm slowly understanding how it works a little more.