Commit f6b2847e authored by Simon McVittie's avatar Simon McVittie

hash test: avoid leaking various keys and values

Signed-off-by: Simon McVittie's avatarSimon McVittie <simon.mcvittie@collabora.co.uk>
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=666115Reviewed-by: Matthias Clasen's avatarMatthias Clasen <mclasen@redhat.com>
parent 5dca72fe
...@@ -861,6 +861,33 @@ set_ref_hash_test (void) ...@@ -861,6 +861,33 @@ set_ref_hash_test (void)
GHashTable *h; 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 static void
value_destroy_insert (gpointer value) value_destroy_insert (gpointer value)
{ {
...@@ -870,18 +897,59 @@ value_destroy_insert (gpointer value) ...@@ -870,18 +897,59 @@ value_destroy_insert (gpointer value)
static void static void
test_destroy_modify (void) test_destroy_modify (void)
{ {
FakeFreeData *ffd;
guint i;
g_test_bug ("650459"); 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")); ffd = g_new0 (FakeFreeData, 1);
g_hash_table_insert (h, g_strdup ("c"), g_strdup ("d")); ffd->string = g_strdup ("a");
g_hash_table_insert (h, g_strdup ("e"), g_strdup ("f")); g_ptr_array_add (fake_free_data, ffd);
g_hash_table_insert (h, g_strdup ("g"), g_strdup ("h")); g_hash_table_insert (h, ffd->string, "c");
g_hash_table_insert (h, g_strdup ("h"), g_strdup ("k"));
g_hash_table_insert (h, g_strdup ("a"), g_strdup ("c"));
g_hash_table_remove (h, "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_remove (h, "e");
g_hash_table_unref (h); g_hash_table_unref (h);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment