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

在 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)

我不知道这个报道是不是假的。

谢谢

4

1 回答 1

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。

于 2013-07-30T13:19:48.920 回答