From 8912f18599c6bc666e587c365fa9160553521622 Mon Sep 17 00:00:00 2001 From: Ralf Habacker Date: Fri, 13 May 2016 01:45:17 +0200 Subject: [PATCH 2/2] Fix memory leak issue in spawn code on Windows. In _dbus_babysitter_block_for_child_exit () use spawning thread handle to have a reliably way to detect spawning thread termination. See https://bugs.freedesktop.org/show_bug.cgi?id=95191#c33 for more informations. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=95191 --- dbus/dbus-spawn-win.c | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/dbus/dbus-spawn-win.c b/dbus/dbus-spawn-win.c index 493b556..1b8f630 100644 --- a/dbus/dbus-spawn-win.c +++ b/dbus/dbus-spawn-win.c @@ -63,10 +63,6 @@ struct DBusBabysitter DBusAtomic refcount; HANDLE start_sync_event; -#ifdef DBUS_ENABLE_EMBEDDED_TESTS - - HANDLE end_sync_event; -#endif char *log_name; @@ -74,6 +70,7 @@ struct DBusBabysitter char **argv; char **envp; + HANDLE thread_handle; HANDLE child_handle; DBusSocket socket_to_babysitter; /* Connection to the babysitter thread */ DBusSocket socket_to_main; @@ -124,13 +121,6 @@ _dbus_babysitter_new (void) return NULL; } - sitter->end_sync_event = CreateEvent (NULL, FALSE, FALSE, NULL); - if (sitter->end_sync_event == NULL) - { - _dbus_babysitter_unref (sitter); - return NULL; - } - sitter->child_handle = NULL; sitter->socket_to_babysitter = sitter->socket_to_main = _dbus_socket_get_invalid (); @@ -267,11 +257,9 @@ _dbus_babysitter_unref (DBusBabysitter *sitter) sitter->start_sync_event = NULL; } - if (sitter->end_sync_event != NULL) - { - CloseHandle (sitter->end_sync_event); - sitter->end_sync_event = NULL; - } + if (sitter->thread_handle) + CloseHandle (sitter->thread_handle); + sitter->thread_handle = NULL; dbus_free (sitter->log_name); @@ -643,8 +631,6 @@ babysitter (void *parameter) _dbus_babysitter_unref (sitter); - SetEvent (sitter->end_sync_event); - return ret ? 0 : 1; } @@ -658,7 +644,6 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p, DBusError *error) { DBusBabysitter *sitter; - HANDLE sitter_thread; DWORD sitter_thread_id; _DBUS_ASSERT_ERROR_IS_CLEAR (error); @@ -729,17 +714,16 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p, sitter->envp = envp; PING(); - sitter_thread = (HANDLE) CreateThread (NULL, 0, babysitter, + sitter->thread_handle = (HANDLE) CreateThread (NULL, 0, babysitter, _dbus_babysitter_ref (sitter), 0, &sitter_thread_id); - if (sitter_thread == 0) + if (sitter->thread_handle == NULL) { PING(); dbus_set_error_const (error, DBUS_ERROR_SPAWN_FORK_FAILED, "Failed to create new thread"); goto out0; } - CloseHandle (sitter_thread); PING(); WaitForSingleObject (sitter->start_sync_event, INFINITE); @@ -801,7 +785,7 @@ get_test_exec (const char *exe, static void _dbus_babysitter_block_for_child_exit (DBusBabysitter *sitter) { - WaitForSingleObject (sitter->end_sync_event, INFINITE); + WaitForSingleObject (sitter->thread_handle, INFINITE); } static dbus_bool_t -- 2.6.6