DBUS服务器崩溃
我正在使用 Glib 绑定实现 DBUS 对象,但在返回 GArray 时遇到问题:
gboolean TestObject_get_data(TestObject* obj, GArray* buffer, GError** error)
{
int i;
printf("%s Entering\n", __PRETTY_FUNCTION__);
buffer = g_array_new(FALSE, FALSE, sizeof(char));
if(buffer)
{
for(i = 0; i < 5 ; i++)
{
g_array_append_val(buffer, i);
}
return TRUE;
}
/* TODO: Error handling */
return FALSE;
}
当我使用测试客户端调用对象的方法时,出现分段错误:
waffleman@thegriddle$ ./testObject
TestObject_get_data Entering
** ERROR **: out of memory
aborting...
Aborted (core dumped)
函数返回后程序崩溃。这是我第一次使用 Glib,所以我可能遗漏了一些明显的东西。我一直在阅读本教程,并且大多数示例都有效。不幸的是,它没有向客户端返回数组的示例。
I am implementing a DBUS object with Glib bindings and am having problems with returning GArrays:
gboolean TestObject_get_data(TestObject* obj, GArray* buffer, GError** error)
{
int i;
printf("%s Entering\n", __PRETTY_FUNCTION__);
buffer = g_array_new(FALSE, FALSE, sizeof(char));
if(buffer)
{
for(i = 0; i < 5 ; i++)
{
g_array_append_val(buffer, i);
}
return TRUE;
}
/* TODO: Error handling */
return FALSE;
}
When I call the object's method with a test client, I get a segmentation fault:
waffleman@thegriddle$ ./testObject
TestObject_get_data Entering
** ERROR **: out of memory
aborting...
Aborted (core dumped)
The program crashes after the function returns. This is the first time I've ever used Glib, so there may be something obvious that I am missing. I have been reading this tutorial, and most of the examples works. Unfortunately it does not have an example for returning an array to the client.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要在函数内分配数组,则需要传入引用。您当前的函数会泄漏内存,因为您在创建数组时分配了局部变量。
If you want to allocate the array inside your function you need to pass in a reference. Your current function leaks memory since you are assigning a local variable when you create the array.