1

我在以下代码中收到上述警告:

[DBus (name = "example.Hello")]
public class HelloDbusServer : Object {
    private bool is_test = false;
    public HelloDbusServer.test() {
        is_test = true;
    }
    [DBus (name = "sayHello")]
    public string say_hello() {
        if (is_test) {
            return "hello (test)";
        }
        return "hello";
    }
}
void on_bus_aquired(DBusConnection conn) {
    try {
        conn.register_object ("/example/Hello", new HelloDbusServer());
    } catch (IOError e) {
        stderr.printf ("Could not register dbus service!\n");
        Posix.exit(1);
    }
}
void on_bus_aquired_test(DBusConnection conn) {
    try {
        conn.register_object ("/example/Hello", new HelloDbusServer.test());
    } catch (IOError e) {
        stderr.printf ("Could not register dbus service!\n");
        Posix.exit(1);
    }
}
void on_bus_name_lost(DBusConnection conn) {
    stderr.printf ("Could not aquire dbus name!\n");
    Posix.exit(2);
}
void main (string[] args) {
    BusType bt = BusType.SYSTEM;
    BusAcquiredCallback cb = on_bus_aquired;
    if ((args.length > 1) && (args[1] == "test"))
    {
        bt = BusType.SESSION;
        cb = on_bus_aquired_test;
        stderr.printf ("Running in test mode on session bus.\n");
    }
    Bus.own_name (bt, "example.Hello", BusNameOwnerFlags.NONE,
                  cb,
                  () => {},
                  on_bus_name_lost);
    new MainLoop().run();
}

在方法调用“Bus.own_name (bt, "example.Hello", BusNameOwnerFlags.NONE, cb, () => {}, on_bus_name_lost)" 处会弹出变量“cb”的警告。

我已经搜索了一个解决方案,并尝试了各种具有“拥有”和闭包的东西,如网络中的一些技巧中提到的,但我没有设法解决这个问题。

谢谢你的帮助。


谢谢你的答案#1。我已经尝试了这两种解决方案。

使用“(拥有)”我得到了这个警告:

    /.../helloFromDBus.vala.c: In function ‘_vala_main’:
    /.../helloFromDBus.vala.c:402:2: warning: passing argument 3 of ‘g_cclosure_new’ from incompatible pointer type [enabled by default]
    /usr/include/glib-2.0/gobject/gclosure.h:206:11: note: expected ‘GClosureNotify’ but argument is of type ‘GDestroyNotify’

我真的不明白这个警告。尝试修复“on_bus_aquired...”方法的签名以符合“BusAcquiredCallback”委托。我添加了“字符串名称”作为第二个参数。然后我得到了与上面相同的警告。

使用 "(con) => { cb (con); }" 会导致错误:

    helloFromDBus.vala:50.18-50.25: error: Too few arguments, method `GLib.BusAcquiredCallback' does not take 1 arguments
              (con) => { cb (con); },

如上所述修复签名并使用 "(con, name) => { cb (con, name); }" 给出以下警告:

    /.../helloFromDBus.vala.c: In function ‘_vala_main’:
    /.../helloFromDBus.vala.c:448:2: warning: passing argument 3 of ‘g_cclosure_new’ from incompatible pointer type [enabled by default]
    /usr/include/glib-2.0/gobject/gclosure.h:206:11: note: expected ‘GClosureNotify’ but argument is of type ‘void (*)(void *)’

我也没有真正理解这个警告。

任何帮助解决这些警告?

4

1 回答 1

4

最好的方法通常是使用以下方式转移您的参考资料(owned)

Bus.own_name (bt,
              "example.Hello",
              BusNameOwnerFlags.NONE,
              (owned) cb,
              () => {},
              on_bus_name_lost);

之后cbnull.

如果这是不可接受的(因为你仍然需要你的副本cb),你可以将回调包装在一个闭包中:

Bus.own_name (bt,
              "example.Hello",
              BusNameOwnerFlags.NONE,
              (con, name) => { cb (con, name); },
              () => {},
              on_bus_name_lost);

不鼓励复制的原因是上下文信息(C 级别的 user_data)没有被引用计数,所以你不能同时拥有两个对它的引用。

于 2013-05-22T16:50:17.297 回答