From f7c24715b5489b28b47499eb252b941b735fa1bc Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Mon, 17 Mar 2003 05:39:10 +0000 Subject: 2003-03-17 Havoc Pennington All tests pass, no memleaks, no valgrind complaints. * bus/test.c: refcount handler_slot * bus/connection.c (bus_connections_new): refcount connection_data_slot * dbus/dbus-auth-script.c (_dbus_auth_script_run): delete unused bytes so that auth scripts pass. * bus/dispatch.c: init message_handler_slot so it gets allocated properly * bus/dispatch.c (message_handler_slot_ref): fix memleak * dbus/dbus-server-debug-pipe.c (_dbus_server_debug_pipe_new): dealloc server_pipe_hash when no longer used for benefit of leak checking * dbus/dbus-auth.c (process_command): memleak fix * bus/dispatch.c (check_hello_message): memleak fix --- bus/connection.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 9 deletions(-) (limited to 'bus/connection.c') diff --git a/bus/connection.c b/bus/connection.c index a8616683..1b59819f 100644 --- a/bus/connection.c +++ b/bus/connection.c @@ -37,6 +37,7 @@ struct BusConnections }; static int connection_data_slot = -1; +static int connection_data_slot_refcount = 0; typedef struct { @@ -51,6 +52,39 @@ typedef struct #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot)) +static dbus_bool_t +connection_data_slot_ref (void) +{ + if (connection_data_slot < 0) + { + connection_data_slot = dbus_connection_allocate_data_slot (); + + if (connection_data_slot < 0) + return FALSE; + + _dbus_assert (connection_data_slot_refcount == 0); + } + + connection_data_slot_refcount += 1; + + return TRUE; + +} + +static void +connection_data_slot_unref (void) +{ + _dbus_assert (connection_data_slot_refcount > 0); + + connection_data_slot_refcount -= 1; + + if (connection_data_slot_refcount == 0) + { + dbus_connection_free_data_slot (connection_data_slot); + connection_data_slot = -1; + } +} + void bus_connection_disconnected (DBusConnection *connection) { @@ -209,6 +243,7 @@ free_connection_data (void *data) if (d->oom_preallocated) dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated); + if (d->oom_message) dbus_message_unref (d->oom_message); @@ -222,17 +257,15 @@ bus_connections_new (BusContext *context) { BusConnections *connections; - if (connection_data_slot < 0) - { - connection_data_slot = dbus_connection_allocate_data_slot (); - - if (connection_data_slot < 0) - return NULL; - } + if (!connection_data_slot_ref ()) + return NULL; connections = dbus_new0 (BusConnections, 1); if (connections == NULL) - return NULL; + { + connection_data_slot_unref (); + return NULL; + } connections->refcount = 1; connections->context = context; @@ -268,7 +301,9 @@ bus_connections_unref (BusConnections *connections) _dbus_list_clear (&connections->list); - dbus_free (connections); + dbus_free (connections); + + connection_data_slot_unref (); } } @@ -286,6 +321,8 @@ bus_connections_setup_connection (BusConnections *connections, d->connections = connections; d->connection = connection; + + _dbus_assert (connection_data_slot >= 0); if (!dbus_connection_set_data (connection, connection_data_slot, @@ -329,6 +366,11 @@ bus_connections_setup_connection (BusConnections *connections, out: if (!retval) { + if (!dbus_connection_set_data (connection, + connection_data_slot, + NULL, NULL)) + _dbus_assert_not_reached ("failed to set connection data to null"); + if (!dbus_connection_set_watch_functions (connection, NULL, NULL, NULL, connection, -- cgit