2
我是新的C語言無法釋放內存
我用dbus_g_bus_get()連接會話管理信號:
static DBusGProxy * connect_to_session (void)
{
DBusGConnection *connection;
DBusGProxy *proxy;
GError *error = NULL;
connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error); /* line 1472 */
if (error) {
g_warning ("Couldn't connect to system bus: %s", error->message);
g_error_free(error);
return NULL;
}
/* Get the current session object */
proxy = dbus_g_proxy_new_for_name (connection,
"org.gnome.SessionManager",
"/org/gnome/SessionManager",
"org.gnome.SessionManager");
if (!proxy) {
g_warning ("Unable to get the SessionManager.");
dbus_g_connection_unref (connection);
return NULL;
}
dbus_g_proxy_add_signal (proxy, "SessionOver", G_TYPE_INVALID, G_TYPE_INVALID);
dbus_g_proxy_connect_signal (proxy, "SessionOver", G_CALLBACK (session_die_cb), NULL, NULL);
g_object_set_data (G_OBJECT (proxy), "connection", connection);
return proxy;
}
調用這個主:
int main(int argc, char* argv[])
{
--------------------------------------------
/* Connect the Session Management signals */
proxy = connect_to_session();
if (proxy) {
DBusGConnection *conn;
conn = (DBusGConnection *)g_object_get_data (G_OBJECT (proxy), "connection");
if (conn)
dbus_g_connection_unref (conn);
g_object_unref (proxy);
}
return 0;
}
和valgrind輸出:
32 bytes in 1 blocks are possibly lost in loss record 5,342 of 13,110
at 0x4C2C6AE: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x6F2ABEE: g_realloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3703.0)
by 0x6CBC577: g_type_set_qdata (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.3703.0)
by 0x513A3D4: ??? (in /usr/lib/x86_64-linux-gnu/libdbus-glib-1.so.2.2.2)
by 0x512F48C: dbus_g_bus_get (in /usr/lib/x86_64-linux-gnu/libdbus-glib-1.so.2.2.2)
by 0x40B669: main (gui.c:1472)
我不知道這份報告是否是虛假的。
感謝
從來沒有使用dbus,但它聽起來像'dbus_g_bus_get'返回一個永遠不會被釋放的對象。 –
+1僅用於*我是C *和使用valgrind的新手。 – Dariusz
在退出之前是否銷燬了'connection'?嘗試調用'dbus_g_connection_unref(連接);'當你不再需要它時。 –