[Libguestfs] [PATCH 0/3] Use __attribute__((cleanup(...)))

Richard W.M. Jones rjones at redhat.com
Fri Jan 25 14:32:15 UTC 2013


This patch series changes a small part of the library to use
__attribute__((cleanup(...))) to automatically free memory when
pointers go out of the current scope.

In general terms this seems to be a small win although you do have to
use it carefully.  For functions where you can completely get rid of
the "exit code paths", it can simplify things.  For a good example,
see the 'inspect-icon.c:icon_windows_xp' function before and after the
change.  For most functions there is no change or a minor
simplification.

It can however cause problems if you accidentally put a CLEANUP_FREE
annotation on a pointer which is actually returned from the function,
or if you explicitly free something.

If we make this change, then the library will leak memory when
compiled with something that isn't GCC/LLVM.  It's likely we have
other dependencies on GCC, since we've never seriously tried compiling
the library with any other compiler.

References
----------

GCC manual:
http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html

Previous attempt:
https://www.redhat.com/archives/libguestfs/2013-January/thread.html#00068






More information about the Libguestfs mailing list