如何在usign gdbus codegen生成代码时堵塞内存泄漏
How do I plug the memory leak when usign gdbus-codegen generated code?
我有一个用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代码生成:
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生成的代码后,我该如何清理?
我怀疑您会发现,如果您在valgrind下使用最新的GLib抑制文件(该文件的版本安装在大多数Linux发行版的/usr/share/glib-2.0/valgrind/
中)运行程序,则不会出现泄漏。正如@potomato在上面的一条评论中所说,信号算作类型数据,GObject永远不会释放它(除了动态注册的类型)。类型数据被分配一次(首次使用时),并且从未释放。别担心。
相关文章:
- 为什么以下C++代码中存在内存泄漏?
- 无法找出我的代码中的内存泄漏
- 如何防止使用 std::shared_ptr 的代码中的内存泄漏
- 为什么当为T2抛出bad_alloc异常时,此代码中会发生内存泄漏
- 以下代码是否存在内存泄漏?
- C++/CLI 代码中的内存泄漏
- 客户端代码中的 Valgrind 钩子:特定函数的内存泄漏
- 我的C++代码中是否有一些内存泄漏
- 为什么 heob 在 QCreator 生成的代码中将这些检测为内存泄漏
- 尝试在我的 C++ 代码中查找内存泄漏
- 以下代码中的嵌入"new"是否会导致内存泄漏?
- 如何解决C++代码中的内存泄漏问题
- 泄漏测试代码shared_ptr奇怪的结果
- 内存泄漏在0行代码中
- 为什么在此OpenCL代码中泄漏内存,为什么要泄漏内存
- QaudioOutput代码是否有任何内存泄漏
- 此代码是否会导致内存泄漏问题
- RAII 失败 - 为什么此C++代码泄漏?- 在尝试块中加入 CTOR 阻止 DTOR
- 这C++代码泄漏内存吗?
- 这个Qt代码泄漏内存