[systemd-devel] libabc, sub-objects, and reference counting

Josh Triplett josh at joshtriplett.org
Sat Dec 6 17:39:32 PST 2014

The sample libabc includes functions to get a "thing", as a sample
sub-object of the overall library context.  Each "thing" has a reference
to the parent library context, and a function to return that reference.
Given that, shouldn't abc_thing_new_from_string call abc_ref, and
abc_thing_unref call abc_unref?

In particular, consider a language binding for a language with garbage
collection, built-in reference counting, or some other mechanism to
avoid requiring explicit calls to _unref functions.  The creation of a
new abc context would call abc_new, and arrange to call abc_unref when
the program no longer needs that abc context.  Similarly, the creation
of a new thing would take an abc context, call abc_thing_new_from_string
(or some similar creation function), and arrange to call abc_thing_unref
when the program no longer needs that thing.  Code in such a language
might create an abc, immediately create a thing, use the thing, and let
all the now-unreferenced objects get destroyed.  If "use the thing"
references the thing but not the abc context, the language could decide
to call abc_unref immediately after creating the thing, then use the
thing, which would have a pointer to an invalid abc context.

- Josh Triplett

More information about the systemd-devel mailing list