[cairo] reference counting vs garbage collection

Jost Boekemeier jost2345 at yahoo.de
Mon Jan 3 05:29:47 PST 2005


I think this is general problem with external resources that user's pass
to cairo.   For any such resource the user needs a callback which
indicates that the resource can be destroyed.  In the case of a file
this would be simply the close() or fclose() callback.


What about a cairo_output_file_t interface which users can implement?  I
think the only methods that this interface needs are  fopen, ferror,
fclose, fwrite.  

This would also solve another problem: on some operating systems it is
not possible to wrap a FILE around a socket; when you read from a
fdopen(sock, "r+") on solaris for example, you get back the bytes you've
just written, not the data from the peer.



> So, if the surface is finalized before the cairo_t and I fclose the file 
> at that point, when the cairo_t gets finalized and the png library 
> attempts to write the file, I get a nice segfault.

I think this indicates that there's a bug in your binding.  The segfault
should not happen if you have a GC.   The fclose callback you've
registered with the garbage collector will be called after all
references to the proxy resource have been dropped.   This may only
happen after all the cairo_t objects referencing the resource have been
destroyed (either in a finalizer or explicit by you). Otherwise you have
a leak which prevents some surfaces from being destroyed (only the
proxies are collected).


Jost





More information about the cairo mailing list