[glib] hash test: avoid leaking various keys and values
- From: Simon McVittie <smcv src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib] hash test: avoid leaking various keys and values
- Date: Wed, 14 Dec 2011 12:54:40 +0000 (UTC)
commit f6b2847e5787a7c17a6d85f57049c5699451c246
Author: Simon McVittie <simon mcvittie collabora co uk>
Date: Tue Dec 13 14:46:02 2011 +0000
hash test: avoid leaking various keys and values
Signed-off-by: Simon McVittie <simon mcvittie collabora co uk>
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=666115
Reviewed-by: Matthias Clasen <mclasen redhat com>
glib/tests/hash.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 75 insertions(+), 7 deletions(-)
---
diff --git a/glib/tests/hash.c b/glib/tests/hash.c
index efc23ef..716bfa2 100644
--- a/glib/tests/hash.c
+++ b/glib/tests/hash.c
@@ -861,6 +861,33 @@ set_ref_hash_test (void)
GHashTable *h;
+typedef struct {
+ gchar *string;
+ gboolean freed;
+} FakeFreeData;
+
+GPtrArray *fake_free_data;
+
+static void
+fake_free (gpointer dead)
+{
+ guint i;
+
+ for (i = 0; i < fake_free_data->len; i++)
+ {
+ FakeFreeData *ffd = g_ptr_array_index (fake_free_data, i);
+
+ if (ffd->string == (gchar *) dead)
+ {
+ g_assert (!ffd->freed);
+ ffd->freed = TRUE;
+ return;
+ }
+ }
+
+ g_assert_not_reached ();
+}
+
static void
value_destroy_insert (gpointer value)
{
@@ -870,18 +897,59 @@ value_destroy_insert (gpointer value)
static void
test_destroy_modify (void)
{
+ FakeFreeData *ffd;
+ guint i;
+
g_test_bug ("650459");
- h = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, value_destroy_insert);
+ fake_free_data = g_ptr_array_new ();
+
+ h = g_hash_table_new_full (g_str_hash, g_str_equal, fake_free, value_destroy_insert);
+
+ ffd = g_new0 (FakeFreeData, 1);
+ ffd->string = g_strdup ("a");
+ g_ptr_array_add (fake_free_data, ffd);
+ g_hash_table_insert (h, ffd->string, "b");
+
+ ffd = g_new0 (FakeFreeData, 1);
+ ffd->string = g_strdup ("c");
+ g_ptr_array_add (fake_free_data, ffd);
+ g_hash_table_insert (h, ffd->string, "d");
+
+ ffd = g_new0 (FakeFreeData, 1);
+ ffd->string = g_strdup ("e");
+ g_ptr_array_add (fake_free_data, ffd);
+ g_hash_table_insert (h, ffd->string, "f");
+
+ ffd = g_new0 (FakeFreeData, 1);
+ ffd->string = g_strdup ("g");
+ g_ptr_array_add (fake_free_data, ffd);
+ g_hash_table_insert (h, ffd->string, "h");
+
+ ffd = g_new0 (FakeFreeData, 1);
+ ffd->string = g_strdup ("h");
+ g_ptr_array_add (fake_free_data, ffd);
+ g_hash_table_insert (h, ffd->string, "k");
- g_hash_table_insert (h, g_strdup ("a"), g_strdup ("b"));
- g_hash_table_insert (h, g_strdup ("c"), g_strdup ("d"));
- g_hash_table_insert (h, g_strdup ("e"), g_strdup ("f"));
- g_hash_table_insert (h, g_strdup ("g"), g_strdup ("h"));
- g_hash_table_insert (h, g_strdup ("h"), g_strdup ("k"));
- g_hash_table_insert (h, g_strdup ("a"), g_strdup ("c"));
+ ffd = g_new0 (FakeFreeData, 1);
+ ffd->string = g_strdup ("a");
+ g_ptr_array_add (fake_free_data, ffd);
+ g_hash_table_insert (h, ffd->string, "c");
g_hash_table_remove (h, "c");
+
+ /* that removed everything... */
+ for (i = 0; i < fake_free_data->len; i++)
+ {
+ FakeFreeData *ffd = g_ptr_array_index (fake_free_data, i);
+
+ g_assert (ffd->freed);
+ g_free (ffd);
+ }
+
+ g_ptr_array_unref (fake_free_data);
+
+ /* ... so this is a no-op */
g_hash_table_remove (h, "e");
g_hash_table_unref (h);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]