Add g_autofree

The g_autoptr() being associated with the type name works out really
well for things like GHashTable.  However, it's a bit more awkward to
associate with "gchar".  Also because one can't use "char".
Similarly, there are a lot of other "bare primitive array" types that
one might reasonably use.

This patch does not remove the autoptr for "gchar", even though I
think it's rather awkward and strange.

Also while we're here, add a test case for the cleanup bits.

https://bugzilla.gnome.org/show_bug.cgi?id=744747
This commit is contained in:
Colin Walters
2015-02-15 08:58:44 -05:00
parent 2844f239f6
commit d0105f1c08
5 changed files with 86 additions and 0 deletions

View File

@@ -2422,6 +2422,40 @@
* Since: 2.44
*/
/**
* g_autofree:
*
* Macro to add an attribute to pointer variable to ensure automatic
* cleanup using g_free().
*
* This macro differs from g_autoptr() in that it is an attribute supplied
* before the type name, rather than wrapping the type definition. Instead
* of using a type-specific lookup, this macro always calls g_free() directly.
*
* This means it's useful for any type that is returned from
* g_malloc().
*
* Otherwise, this macro has similar constraints as g_autoptr() - only
* supported on GCC and clang, the variable must be initialized, etc.
*
* |[
* gboolean
* operate_on_malloc_buf (void)
* {
* g_autofree guint8* membuf = NULL;
*
* membuf = g_malloc (8192);
*
* /* Some computation on membuf */
*
* /* membuf will be automatically freed here */
* return TRUE;
* }
* ]|
*
* Since: 2.44
*/
/**
* G_DEFINE_AUTOPTR_CLEANUP_FUNC:
* @TypeName: a type name to define a g_autoptr() cleanup function for