1

我有一个以 DBus Introspection XML 格式描述的接口:

<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
                      "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
  <interface name="com.example.foo">
    <method name="Bar" />
  </interface>
</node>

我像这样运行 gdbus-codegen:

gdbus-codegen --interface-prefix=com.example --generate-c-code=foo-dbus --c-namespace=Dbus foo.xml

我使用这样的代码:

int main()
{
    DbusFoo * skeleton = dbus_foo_skeleton_new();
    g_object_unref(skeleton);

    return 0;
}

但是应用程序最终泄漏了一个生成的信号dbus_foo_default_init(),如下所示:

static void
dbus_foo_default_init (DbusFooIface *iface)
{
  /* GObject signals for incoming D-Bus method calls: */
  /**
   * DbusFoo::handle-bar:
   * @object: A #DbusFoo.
   * @invocation: A #GDBusMethodInvocation.
   *
   * Signal emitted when a remote caller is invoking the <link linkend="gdbus-method-com-example-foo.Bar">Bar()</link> D-Bus method.
   *
   * If a signal handler returns %TRUE, it means the signal handler will handle the invocation (e.g. take a reference to @invocation and eventually call dbus_foo_complete_bar() or e.g. g_dbus_method_invocation_return_error() on it) and no order signal handlers will run. If no signal handler handles the invocation, the %G_DBUS_ERROR_UNKNOWN_METHOD error is returned.
   *
   * Returns: %TRUE if the invocation was handled, %FALSE to let other signal handlers run.
   */
  g_signal_new ("handle-bar",
    G_TYPE_FROM_INTERFACE (iface),
    G_SIGNAL_RUN_LAST,
    G_STRUCT_OFFSET (DbusFooIface, handle_bar),
    g_signal_accumulator_true_handled,
    NULL,
    g_cclosure_marshal_generic,
    G_TYPE_BOOLEAN,
    1,
    G_TYPE_DBUS_METHOD_INVOCATION);

}

我的问题:

使用 gdbus-codegen 生成的代码后如何清理?

4

1 回答 1

1

我怀疑你会发现,如果你在 valgrind 下使用最新的 GLib 抑制文件(大多数 Linux 发行版上都安装了该文件的一个版本)运行你的程序/usr/share/glib-2.0/valgrind/,就没有泄漏。正如@ptomato 在上面的评论中所说,信号算作类型数据,而 GObject 永远不会释放它(除了动态注册的类型)。类型数据被分配一次(第一次使用时)并且永远不会被释放。别担心。

于 2017-10-03T08:20:29.517 回答