无法释放内存

时间:2013-07-30 11:34:05

标签: c dbus

我是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;
}

在main中调用它:

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)

我不知道这份报告是否是假的。

由于

1 个答案:

答案 0 :(得分:0)

Valgrind有一些处理glbal变量的问题,Stackoverflow上有很多帖子。您正在调用dbus_g_bus_get,并且返回对于全局变量更为精确。

DBusGConnection* dbus_g_bus_get (DBusBusType type, GError **error);

Returns a connection to the given bus. The connection is a global variable shared with other callers of this function. 

您也可以在获得连接后尝试调用dbus_g_connection_ref。