glib/tests: fix leaks

https://bugzilla.gnome.org/show_bug.cgi?id=682560
This commit is contained in:
Dan Winship 2012-08-23 12:48:49 -04:00
parent 0c0cdfd9c4
commit 1a2c5e155d
19 changed files with 574 additions and 472 deletions

View File

@ -167,6 +167,8 @@ test_async_queue_threads (void)
g_assert_cmpint (s, ==, total); g_assert_cmpint (s, ==, total);
g_assert_cmpint (c, ==, 1000); g_assert_cmpint (c, ==, 1000);
g_async_queue_unref (q);
} }
static void static void
@ -201,6 +203,8 @@ test_async_queue_timed (void)
diff = end - start; diff = end - start;
g_assert_cmpint (diff, >=, G_USEC_PER_SEC / 10); g_assert_cmpint (diff, >=, G_USEC_PER_SEC / 10);
g_assert_cmpint (diff, <, G_USEC_PER_SEC); g_assert_cmpint (diff, <, G_USEC_PER_SEC);
g_async_queue_unref (q);
} }
int int

View File

@ -287,7 +287,8 @@ main (int argc, char *argv[])
while ((name = g_dir_read_name (dir)) != NULL) while ((name = g_dir_read_name (dir)) != NULL)
{ {
path = g_strdup_printf ("/bookmarks/parse/%s", name); path = g_strdup_printf ("/bookmarks/parse/%s", name);
g_test_add_data_func (path, g_build_filename (SRCDIR, "bookmarks", name, NULL), test_file); g_test_add_data_func_full (path, g_build_filename (SRCDIR, "bookmarks", name, NULL),
test_file, g_free);
g_free (path); g_free (path);
} }
g_dir_close (dir); g_dir_close (dir);

View File

@ -752,15 +752,18 @@ add_checksum_test (GChecksumType type,
{ {
ChecksumTest *test; ChecksumTest *test;
gchar *path; gchar *path;
test = g_new0 (ChecksumTest, 1); test = g_new0 (ChecksumTest, 1);
test->checksum_type = type; test->checksum_type = type;
test->sum = sum; test->sum = sum;
test->length = length; test->length = length;
path = g_strdup_printf ("/checksum/%s/%d", type_name, length); path = g_strdup_printf ("/checksum/%s/%d", type_name, length);
g_test_add_data_func (path, test, test_checksum); g_test_add_data_func (path, test, test_checksum);
g_free (path); g_free (path);
path = g_strdup_printf ("/checksum/%s/reset/%d", type_name, length); path = g_strdup_printf ("/checksum/%s/reset/%d", type_name, length);
g_test_add_data_func (path, test, test_checksum_reset); g_test_add_data_func_full (path, test, test_checksum_reset, g_free);
g_free (path); g_free (path);
} }
@ -771,11 +774,13 @@ add_checksum_string_test (GChecksumType type,
{ {
ChecksumComputeTest *test; ChecksumComputeTest *test;
gchar *path; gchar *path;
test = g_new0 (ChecksumComputeTest, 1); test = g_new0 (ChecksumComputeTest, 1);
test->checksum_type = type; test->checksum_type = type;
test->sums = sums; test->sums = sums;
path = g_strdup_printf ("/checksum/%s/string", type_name); path = g_strdup_printf ("/checksum/%s/string", type_name);
g_test_add_data_func (path, test, test_checksum_string); g_test_add_data_func_full (path, test, test_checksum_string, g_free);
g_free (path); g_free (path);
} }
@ -786,11 +791,13 @@ add_checksum_bytes_test (GChecksumType type,
{ {
ChecksumComputeTest *test; ChecksumComputeTest *test;
gchar *path; gchar *path;
test = g_new0 (ChecksumComputeTest, 1); test = g_new0 (ChecksumComputeTest, 1);
test->checksum_type = type; test->checksum_type = type;
test->sums = sums; test->sums = sums;
path = g_strdup_printf ("/checksum/%s/bytes", type_name); path = g_strdup_printf ("/checksum/%s/bytes", type_name);
g_test_add_data_func (path, test, test_checksum_bytes); g_test_add_data_func_full (path, test, test_checksum_bytes, g_free);
g_free (path); g_free (path);
} }

View File

@ -10,10 +10,16 @@ typedef struct {
} CollateTest; } CollateTest;
typedef struct { typedef struct {
const gchar *key; gchar *key;
const gchar *str; const gchar *str;
} Line; } Line;
static void
clear_line (Line *line)
{
g_free (line->key);
}
static int static int
compare_collate (const void *a, const void *b) compare_collate (const void *a, const void *b)
{ {
@ -35,10 +41,13 @@ compare_key (const void *a, const void *b)
static void static void
do_collate (gboolean for_file, gboolean use_key, const CollateTest *test) do_collate (gboolean for_file, gboolean use_key, const CollateTest *test)
{ {
GArray *line_array = g_array_new (FALSE, FALSE, sizeof(Line)); GArray *line_array;
Line line; Line line;
gint i; gint i;
line_array = g_array_new (FALSE, FALSE, sizeof(Line));
g_array_set_clear_func (line_array, (GDestroyNotify)clear_line);
for (i = 0; test->input[i]; i++) for (i = 0; test->input[i]; i++)
{ {
line.str = test->input[i]; line.str = test->input[i];
@ -61,6 +70,8 @@ do_collate (gboolean for_file, gboolean use_key, const CollateTest *test)
else else
g_assert_cmpstr (str, ==, test->sorted[i]); g_assert_cmpstr (str, ==, test->sorted[i]);
} }
g_array_free (line_array, TRUE);
} }
static void static void

View File

@ -170,6 +170,13 @@ barrier_wait (Barrier *barrier)
return ret; return ret;
} }
static void
barrier_clear (Barrier *barrier)
{
g_mutex_clear (&barrier->mutex);
g_cond_clear (&barrier->cond);
}
static Barrier b; static Barrier b;
static gint check; static gint check;
@ -220,6 +227,8 @@ test_cond2 (void)
g_thread_join (threads[i]); g_thread_join (threads[i]);
g_assert_cmpint (g_atomic_int_get (&check), ==, 10); g_assert_cmpint (g_atomic_int_get (&check), ==, 10);
barrier_clear (&b);
} }
int int

View File

@ -699,6 +699,7 @@ test_no_conv (void)
/* error code is unreliable, since we mishandle errno there */ /* error code is unreliable, since we mishandle errno there */
g_assert (error && error->domain == G_CONVERT_ERROR); g_assert (error && error->domain == G_CONVERT_ERROR);
g_error_free (error);
} }
int int

View File

@ -125,6 +125,8 @@ test_dataset_foreach (void)
g_dataset_set_data_full (location, "test3", "test3", notify); g_dataset_set_data_full (location, "test3", "test3", notify);
g_dataset_foreach (location, foreach, &my_count); g_dataset_foreach (location, foreach, &my_count);
g_assert (my_count == 3); g_assert (my_count == 3);
g_dataset_destroy (location);
} }
static void static void

View File

@ -669,6 +669,7 @@ test_file_open_tmp (void)
g_assert_no_error (error); g_assert_no_error (error);
g_assert (name != NULL); g_assert (name != NULL);
unlink (name); unlink (name);
g_free (name);
close (fd); close (fd);
fd = g_file_open_tmp (NULL, &name, &error); fd = g_file_open_tmp (NULL, &name, &error);
@ -676,6 +677,7 @@ test_file_open_tmp (void)
g_assert_no_error (error); g_assert_no_error (error);
g_assert (name != NULL); g_assert (name != NULL);
g_unlink (name); g_unlink (name);
g_free (name);
close (fd); close (fd);
name = NULL; name = NULL;

View File

@ -107,6 +107,7 @@ context_clear (struct context *ctx)
g_assert (ctx->pending_tokens == NULL); g_assert (ctx->pending_tokens == NULL);
g_assert (ctx->quit); g_assert (ctx->quit);
g_mutex_clear (&ctx->lock);
g_wakeup_free (ctx->wakeup); g_wakeup_free (ctx->wakeup);
} }

View File

@ -247,6 +247,16 @@ test_priorities (void)
g_main_context_unref (ctx); g_main_context_unref (ctx);
} }
static gboolean
quit_loop (gpointer data)
{
GMainLoop *loop = data;
g_main_loop_quit (loop);
return G_SOURCE_REMOVE;
}
static gint count; static gint count;
static gboolean static gboolean
@ -276,9 +286,11 @@ static gpointer
thread_func (gpointer data) thread_func (gpointer data)
{ {
GMainContext *ctx = data; GMainContext *ctx = data;
GMainLoop *loop;
GSource *source; GSource *source;
g_main_context_push_thread_default (ctx); g_main_context_push_thread_default (ctx);
loop = g_main_loop_new (ctx, FALSE);
g_mutex_lock (&mutex); g_mutex_lock (&mutex);
thread_ready = TRUE; thread_ready = TRUE;
@ -286,12 +298,14 @@ thread_func (gpointer data)
g_mutex_unlock (&mutex); g_mutex_unlock (&mutex);
source = g_timeout_source_new (500); source = g_timeout_source_new (500);
g_source_set_callback (source, (GSourceFunc)g_thread_exit, NULL, NULL); g_source_set_callback (source, quit_loop, loop, NULL);
g_source_attach (source, ctx); g_source_attach (source, ctx);
g_source_unref (source); g_source_unref (source);
while (TRUE) g_main_loop_run (loop);
g_main_context_iteration (ctx, TRUE);
g_main_context_pop_thread_default (ctx);
g_main_loop_unref (loop);
return NULL; return NULL;
} }
@ -328,16 +342,8 @@ test_invoke (void)
g_thread_join (thread); g_thread_join (thread);
g_assert_cmpint (count, ==, 3); g_assert_cmpint (count, ==, 3);
}
static gboolean g_main_context_unref (ctx);
quit_loop (gpointer data)
{
GMainLoop *loop = data;
g_main_loop_quit (loop);
return G_SOURCE_REMOVE;
} }
static gboolean static gboolean
@ -353,6 +359,7 @@ run_inner_loop (gpointer user_data)
timeout = g_timeout_source_new (100); timeout = g_timeout_source_new (100);
g_source_set_callback (timeout, quit_loop, inner, NULL); g_source_set_callback (timeout, quit_loop, inner, NULL);
g_source_attach (timeout, ctx); g_source_attach (timeout, ctx);
g_source_unref (timeout);
g_main_loop_run (inner); g_main_loop_run (inner);
g_main_loop_unref (inner); g_main_loop_unref (inner);
@ -429,6 +436,7 @@ test_child_sources (void)
g_assert_cmpint (b, ==, 3); g_assert_cmpint (b, ==, 3);
g_assert_cmpint (c, ==, 3); g_assert_cmpint (c, ==, 3);
g_source_destroy (parent);
g_source_unref (parent); g_source_unref (parent);
g_source_unref (child_b); g_source_unref (child_b);
g_source_unref (child_c); g_source_unref (child_c);
@ -485,6 +493,7 @@ test_recursive_child_sources (void)
g_assert_cmpint (b, ==, 9); g_assert_cmpint (b, ==, 9);
g_assert_cmpint (c, ==, 4); g_assert_cmpint (c, ==, 4);
g_source_destroy (parent);
g_source_unref (parent); g_source_unref (parent);
g_source_unref (child_b); g_source_unref (child_b);
g_source_unref (child_c); g_source_unref (child_c);

View File

@ -304,7 +304,8 @@ main (int argc, char *argv[])
continue; continue;
path = g_strdup_printf ("/markup/parse/%s", name); path = g_strdup_printf ("/markup/parse/%s", name);
g_test_add_data_func (path, g_build_filename (SRCDIR, "markups", name, NULL), test_parse); g_test_add_data_func_full (path, g_build_filename (SRCDIR, "markups", name, NULL),
test_parse, g_free);
g_free (path); g_free (path);
} }
g_dir_close (dir); g_dir_close (dir);

View File

@ -2119,7 +2119,7 @@ test_group_parse (void)
{ "faz", 'z', 0, G_OPTION_ARG_STRING, &arg5, NULL, NULL }, { "faz", 'z', 0, G_OPTION_ARG_STRING, &arg5, NULL, NULL },
{ NULL } { NULL }
}; };
gchar **argv; gchar **argv, **orig_argv;
gint argc; gint argc;
GError *error = NULL; GError *error = NULL;
gboolean retval; gboolean retval;
@ -2131,7 +2131,10 @@ test_group_parse (void)
g_option_context_add_group (context, group); g_option_context_add_group (context, group);
argv = split_string ("program --test arg1 -f arg2 --group-test arg3 --frob arg4 -z arg5", &argc); argv = split_string ("program --test arg1 -f arg2 --group-test arg3 --frob arg4 -z arg5", &argc);
orig_argv = g_memdup (argv, (argc + 1) * sizeof (char *));
retval = g_option_context_parse (context, &argc, &argv, &error); retval = g_option_context_parse (context, &argc, &argv, &error);
g_assert_no_error (error); g_assert_no_error (error);
g_assert (retval); g_assert (retval);
g_assert_cmpstr (arg1, ==, "arg1"); g_assert_cmpstr (arg1, ==, "arg1");
@ -2145,6 +2148,9 @@ test_group_parse (void)
g_free (arg3); g_free (arg3);
g_free (arg4); g_free (arg4);
g_free (arg5); g_free (arg5);
g_free (argv);
g_strfreev (orig_argv);
g_option_context_free (context); g_option_context_free (context);
} }

View File

@ -498,8 +498,9 @@ random_test (gconstpointer d)
case REMOVE: case REMOVE:
if (!g_queue_is_empty (q)) if (!g_queue_is_empty (q))
g_queue_remove (q, qinf->tail->data); g_queue_remove (q, qinf->tail->data);
/* qinf->head/qinf->tail may be invalid at this point */
if (!g_queue_is_empty (q)) if (!g_queue_is_empty (q))
g_queue_remove (q, qinf->head->data); g_queue_remove (q, q->head->data);
if (!g_queue_is_empty (q)) if (!g_queue_is_empty (q))
g_queue_remove (q, g_queue_peek_nth (q, get_random_position (q, TRUE))); g_queue_remove (q, g_queue_peek_nth (q, get_random_position (q, TRUE)));
@ -510,8 +511,9 @@ random_test (gconstpointer d)
case REMOVE_ALL: case REMOVE_ALL:
if (!g_queue_is_empty (q)) if (!g_queue_is_empty (q))
g_queue_remove_all (q, qinf->tail->data); g_queue_remove_all (q, qinf->tail->data);
/* qinf->head/qinf->tail may be invalid at this point */
if (!g_queue_is_empty (q)) if (!g_queue_is_empty (q))
g_queue_remove_all (q, qinf->head->data); g_queue_remove_all (q, q->head->data);
if (!g_queue_is_empty (q)) if (!g_queue_is_empty (q))
g_queue_remove_all (q, g_queue_peek_nth (q, get_random_position (q, TRUE))); g_queue_remove_all (q, g_queue_peek_nth (q, get_random_position (q, TRUE)));

File diff suppressed because it is too large Load Diff

View File

@ -215,6 +215,7 @@ main (int argc,
char *argv[]) char *argv[])
{ {
char *dirname; char *dirname;
int ret;
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
@ -232,5 +233,9 @@ main (int argc,
g_test_add_func ("/gthread/spawn-sync", test_spawn_sync_multithreaded); g_test_add_func ("/gthread/spawn-sync", test_spawn_sync_multithreaded);
g_test_add_func ("/gthread/spawn-async", test_spawn_async_multithreaded); g_test_add_func ("/gthread/spawn-async", test_spawn_async_multithreaded);
return g_test_run(); ret = g_test_run();
g_free (echo_prog_path);
return ret;
} }

View File

@ -189,6 +189,7 @@ main (int argc,
char *argv[]) char *argv[])
{ {
char *dirname; char *dirname;
int ret;
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
@ -221,5 +222,10 @@ main (int argc,
g_test_add_func ("/gthread/spawn-single-async", test_spawn_async); g_test_add_func ("/gthread/spawn-single-async", test_spawn_async);
g_test_add_func ("/gthread/spawn-script", test_spawn_script); g_test_add_func ("/gthread/spawn-script", test_spawn_script);
return g_test_run(); ret = g_test_run();
g_free (echo_script_path);
g_free (echo_prog_path);
return ret;
} }

View File

@ -490,6 +490,8 @@ test_string_to_bytes (void)
g_assert_cmpint (byte_len, ==, 7); g_assert_cmpint (byte_len, ==, 7);
g_assert_cmpint (memcmp (byte_data, "foo-bar", byte_len), ==, 0); g_assert_cmpint (memcmp (byte_data, "foo-bar", byte_len), ==, 0);
g_bytes_unref (bytes);
} }
int int

View File

@ -22,6 +22,8 @@ function (gpointer data)
static void static void
test_seconds (void) test_seconds (void)
{ {
guint id;
/* Bug 642052 mentions that g_timeout_add_seconds(21475) schedules a /* Bug 642052 mentions that g_timeout_add_seconds(21475) schedules a
* job that runs once per second. * job that runs once per second.
* *
@ -40,10 +42,12 @@ test_seconds (void)
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
g_timeout_add (2100, stop_waiting, NULL); g_timeout_add (2100, stop_waiting, NULL);
g_timeout_add_seconds (21475, function, NULL); id = g_timeout_add_seconds (21475, function, NULL);
g_main_loop_run (loop); g_main_loop_run (loop);
g_main_loop_unref (loop); g_main_loop_unref (loop);
g_source_remove (id);
} }
static gint64 last_time; static gint64 last_time;

View File

@ -96,6 +96,7 @@ static void
test_signal (int signum) test_signal (int signum)
{ {
GMainLoop *mainloop; GMainLoop *mainloop;
int id;
mainloop = g_main_loop_new (NULL, FALSE); mainloop = g_main_loop_new (NULL, FALSE);
@ -103,10 +104,11 @@ test_signal (int signum)
g_unix_signal_add (signum, on_sig_received, mainloop); g_unix_signal_add (signum, on_sig_received, mainloop);
kill (getpid (), signum); kill (getpid (), signum);
g_assert (!sig_received); g_assert (!sig_received);
g_timeout_add (5000, sig_not_received, mainloop); id = g_timeout_add (5000, sig_not_received, mainloop);
g_main_loop_run (mainloop); g_main_loop_run (mainloop);
g_assert (sig_received); g_assert (sig_received);
sig_received = FALSE; sig_received = FALSE;
g_source_remove (id);
/* Ensure we don't get double delivery */ /* Ensure we don't get double delivery */
g_timeout_add (500, exit_mainloop, mainloop); g_timeout_add (500, exit_mainloop, mainloop);