[cairo] Language Binding Considerations: Memory Management

Lawrence D'Oliveiro ldo at geek-central.gen.nz
Sun Mar 29 21:37:46 PDT 2015


In <http://cairographics.org/manual/bindings-memory.html>, you talk
about maintaining a “pin table” that maps from Cairo objects to language
wrapper objects, to ensure that the same Cairo object always maps to
the same language wrapper object. This is a good idea. But I think you
should also mention that the table should contain weak references to
the wrapper objects, where the language supports it.

This is what I do in Qahirah. Python conveniently provides a
“WeakValueDictionary”
<https://docs.python.org/3/library/weakref.html#weakref.WeakValueDictionary>,
with entries that are automatically deleted when the objects being
weakly-referenced disappear. I customize the constructors for the
wrapper objects to look the Cairo objects up in this table and only
create a new wrapper object if one cannot already be found. This also
seems to solve the derivation problem--at least as long as the user
keeps their own reference to the wrapper object.


More information about the cairo mailing list