diff --git a/glib/glib-autocleanups.h b/glib/glib-autocleanups.h index 8b80b3853..cab6408e8 100644 --- a/glib/glib-autocleanups.h +++ b/glib/glib-autocleanups.h @@ -29,8 +29,15 @@ g_autoptr_cleanup_generic_gfree (void *p) g_free (*pp); } +static inline void +g_autoptr_cleanup_gstring_free (GString *string) +{ + if (string) + g_string_free (string, TRUE); +} + /* If adding a cleanup here, please also add a test case to - * glib/glib/autoptr.c + * glib/tests/autoptr.c */ G_DEFINE_AUTOPTR_CLEANUP_FUNC(GAsyncQueue, g_async_queue_unref) G_DEFINE_AUTOPTR_CLEANUP_FUNC(GBookmarkFile, g_bookmark_file_free) @@ -64,6 +71,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(GMatchInfo, g_match_info_unref) G_DEFINE_AUTOPTR_CLEANUP_FUNC(GScanner, g_scanner_destroy) G_DEFINE_AUTOPTR_CLEANUP_FUNC(GSequence, g_sequence_free) G_DEFINE_AUTOPTR_CLEANUP_FUNC(GSList, g_slist_free) +G_DEFINE_AUTOPTR_CLEANUP_FUNC(GString, g_autoptr_cleanup_gstring_free) G_DEFINE_AUTOPTR_CLEANUP_FUNC(GStringChunk, g_string_chunk_free) G_DEFINE_AUTOPTR_CLEANUP_FUNC(GThread, g_thread_unref) G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GMutex, g_mutex_clear) diff --git a/glib/tests/autoptr.c b/glib/tests/autoptr.c index 676ee0733..6f0eafbf8 100644 --- a/glib/tests/autoptr.c +++ b/glib/tests/autoptr.c @@ -276,6 +276,13 @@ test_g_slist (void) g_assert (nonempty_val != NULL); } +static void +test_g_string (void) +{ + g_autoptr(GString) val = g_string_new (""); + g_assert (val != NULL); +} + static void test_g_string_chunk (void) { @@ -428,6 +435,7 @@ main (int argc, gchar *argv[]) g_test_add_func ("/autoptr/g_scanner", test_g_scanner); g_test_add_func ("/autoptr/g_sequence", test_g_sequence); g_test_add_func ("/autoptr/g_slist", test_g_slist); + g_test_add_func ("/autoptr/g_string", test_g_string); g_test_add_func ("/autoptr/g_string_chunk", test_g_string_chunk); g_test_add_func ("/autoptr/g_thread", test_g_thread); g_test_add_func ("/autoptr/g_mutex", test_g_mutex);