【问题标题】:How to get rid of the vala compilation warning "copying delegates is discouraged" in the right way?如何以正确的方式摆脱vala编译警告“不鼓励复制代表”?
【发布时间】:2013-05-25 02:56:46
【问题描述】:

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

[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 *)’

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

对修复这些警告有什么帮助吗?

【问题讨论】:

  • 你到底尝试了什么?
  • 请查看已编辑的问题。
  • 不要担心那些警告。这只是 C 编译器的抱怨,它应该可以正常工作。如果需要,可以将 -X -w 传递给 valac 以禁用来自 C 编译器的警告。 live.gnome.org/Vala/FAQ#How_can_I_fix_CC_warnings.3F
  • 感谢您的建议/提示。

标签: vala


【解决方案1】:

最好的方法通常是使用(owned) 转移您的参考:

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

之后cb 将是null

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

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

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

【讨论】:

    猜你喜欢
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 2019-12-18
    相关资源
    最近更新 更多