[cairo] reference counting vs garbage collection

Keith Packard keithp at keithp.com
Tue Jan 4 20:29:43 PST 2005


Around 22 o'clock on Jan 4, Carl Worth wrote:

> So, I propose an _unref() function as above, and a _destroy() function
> which is equivalent to the pair of _close() and _unref() as described
> above.
> 
> Does that seem workable? Or am I still totally missing some
> GC-language binding issues?

Yeah, the GC language issue is that the order in which 'unref' calls occur
isn't deterministic -- collect a 'cairo_t' and it will 'unref' the 
surface, collect a 'cairo_surface_t' and it will also 'unref' the surface, 
and presumably clean up any dependent objects (like FILEs).  If the 
surface is collected first, then when the cairo_t is collected, the 
dependent objects will already have been destroyed.

Here's what I think each function should do:

    _create	allocate object. Refcnt = 1.  Connect dependent objects
    _ref	refcnt++
    _unref	if (--refcnt = 0) { finalize dependent objects; free object }
    _destroy	finalize dependent objects; _unref

This way _destroy deterministically finalizes the dependent objets, even 
if there are more references.  unref, on the other hand, just drops a 
reference, finalizing when needed as the reference hits 0.

So, programs would always do either:

	_create
	...
	_destroy
	fclose ();

or
	_ref
	...
	_unref

and these sequences would always work.

-keith


-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
Url : http://lists.freedesktop.org/archives/cairo/attachments/20050104/1e917782/attachment.pgp


More information about the cairo mailing list