Memory leak in g_dbus_connection_close_sync()
Submitted by Adrian Szyndela
Assigned to David Zeuthen
Link to original bug (#742856)
Description
Created attachment 294432 My patch for memory leak in g_dbus_connection_close_sync()
Hi, let's consider simple code that creates and closes a connection: GError *error = NULL; gchar *addr = NULL; GDBusConnection *connection = NULL;
addr = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
connection = g_dbus_connection_new_for_address_sync(addr,
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
NULL, NULL, &error);
g_dbus_connection_close_sync(connection, NULL, NULL);
g_object_unref(connection);
At this point, connection should have been freed, right?. However, ref_count is still 1. When we go into details it appears that the ref comes from main context that is in connection->main_context_at_construction. That one, in turn, has ref_count 11 (in my case), because of sources connected to the context somewhere inside g_dbus_connection_close_sync().
It appears there are three different main-contexts involved: the one in 'gdbus' thread, connection->main_context_at_construction and the one created in g_dbus_connection_close_sync(). However, only connection->main_context_at_construction seems to be a context that no iteration is performed on.
Sources are simply added to this context, and then nothing more happens to them.
For my needs, I resolved this by putting a line: g_main_context_iteration(connection->main_context_at_construction, FALSE);
after g_dbus_connection_close_finish() call (see attached patch).
However, I am new to glib, and I simply don't know if it is "correct" solution.
Patch 294432, "My patch for memory leak in g_dbus_connection_close_sync()":
gdbus-connection-main-context-iteration-in-close-sync.patch
Version: 2.42.x