[cairo] reference counting vs garbage collection

Owen Taylor otaylor at redhat.com
Wed Jan 5 10:57:13 PST 2005


On Wed, 2005-01-05 at 13:52 -0500, Carl Worth wrote:
> On Wed, 05 Jan 2005 11:02:12 -0500, Owen Taylor wrote:
> > > On Tue, 04 Jan 2005 20:29:43 -0800, Keith Packard wrote:
> > > > 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
> > 
> > I think making destroy unref in this manner is a serious mistake,
> > because it makes things *very* hard for language bindings;
> 
> _destroy must unref since it's the balancing call for _create --- it's
> our explicit destructor. It's not just a poorly named finish function.

Well, currently, destroy() is just your weirdly named unref. I'm not
*happy* about that, but it's acceptable. But it should have no
other semantic beyond unref.

> > for a language
> > binding, reference counting (keeping memory alive) must be independent
> > from method called by the application.
> 
> I agree with that.
> 
> I guess my mental block is that I can't see why an application would
> want to call a "finish" function. It sounds to me like, "Render this
> object useless, but let me still hold on to it".

Why would a file object in a ref-counted or GC'ed language have a close
() 
operation? Most do. Sometimes it isn't convenient to get rid of all
references to an object precisely when you need to do whatever side
effects that might normally done on finalization.

Consider, finishing writing a PDF file, then immediately wanting to
call an external program to display it ... we want the end of the
PDF file written and the underlying file flushed and closed at a 
deterministic point a time.

There's actually one possible use case for a close() for Xlib
surfaces. I'd rather that:

 surface = window.get_cairo_surface()
 window.destroy() [ Calls XDestroyWindow ]
 [ use surface ]

Just was a no-op rather than an X error or segfault. We try to 
make sure that using GDK windows after they are destroyed silently
does nothing (note destroyed, not refcounted to zero). This prevents
various tricky reentrancy problems.

> Regardless, it does seem easy enough to remove the conflation:
> 
> _create		Allocate object. refcnt = 1. Connect dependent objects
> _reference	refcnt++
> _finish		Finalize dependent objects
> _destroy	if (--refcnt = 0) { _finish; Free object }
> 
> That maintains the pairing of _reference/_destroy as with the current
> code. And it allows Keith to get deterministic finalization, (he just
> needs to add a call to _finish before _destroy).

Seems OK to me. [pervasive skepticism about FILE * handling omitted]

Regards,
					Owen

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://lists.freedesktop.org/archives/cairo/attachments/20050105/bd80e779/attachment.pgp


More information about the cairo mailing list