Reputation: 4339
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.
Upvotes: 1
Views: 375
Reputation: 814
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.
gboolean TestObject_get_data(TestObject *obj, GArray **buffer, GError **error)
{
if (buffer) {
*buffer = g_array_new(FALSE, FALSE, sizeof(char));
if (*buffer) {
int i;
for (i = 0; i < 5 ; ++i) {
g_array_append_val(*buffer, i);
}
return TRUE;
}
}
return FALSE;
}
Upvotes: 1