On 8/23/19 11:39 AM, Daniel P. Berrangé wrote: > Document the use of g_autofree and g_autoptr in glib for automatic > freeing of memory, or other resource cleanup (eg mutex unlocking). > > Signed-off-by: Daniel P. Berrangé > --- > CODING_STYLE.md | 101 ++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 101 insertions(+) > +The cleanup functions are not restricted to simply free'ing memory. The > +GMutexLocker class is a variant of GMutex that has automatic locking and > +unlocking at start and end of the enclosing scope > + > +In the following example, the `lock` in `MyObj` will be held for the > +precise duration of the `somefunc` function > + > + typedef struct { > + GMutex lock; > + } MyObj; > + > + char *somefunc(MyObj *obj) { > + g_autofree GMutexLocker *locker = g_mutex_locker_new(&obj->lock) Wrong example (you don't want to call g_free, and you missed ';'). This should be g_autoptr (GMutexLocker) locker = g_mutex_locker_new(&obj->lock); With that fixed, Reviewed-by: Eric Blake -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org