Unable to iterate a GVariant with array type using iterator

1.9k Views Asked by At

I am trying to call a remote method over DBus using GDBus. My problem is that the method call is successful, but the return value from the method, available as a GVariant contains an array as its element. When I try to iterate through it using an GVariantIter, it causes segmentation fault. But if I print it to stdout using g_variant_print() it is successfully printing the data. What am I doing wrong ? The format string specified is correct and I have checked it using g_variant_get_type_string(). My code snippet is given below.

        GVariantIter* iter;
        gint16 res, mts, vlm;
        guint16 sid;
        char * sname ;
        GVariant* avail ;
        guint16 clsid;
        GVariant* retvalue = g_dbus_proxy_call_sync (proxy, 
                                    "Method", 
                                    "", //no parameters
                                    G_DBUS_CALL_FLAGS_NONE, 
                                    -1, 
                                    NULL, 
                                    &error 
                                  );

        g_variant_get(retvalue, "(na(qs(nn)nnq))",&res, &iter);
        while ( g_variant_iter_loop (iter, "(qs(nn)nnq)", &sid, &sname, &avail, &mts, &vlm,  &clsid) )
        {
            // Operate on the variables
        }
1

There are 1 best solutions below

0
On

g_dbus_proxy_call_sync() returns your GVariant inside a tuple if I am not mistaken. What you need to do is to open the tuple before you create your iterator.

Try to do this instead after you called g_dbus_proxy_call_sync:

GVariantIter *iter;
GVariant *temp;

temp = g_variant_get_child_value (retvalue, 0);

iter = g_variant_iter_new (temp);

while (g_variant_iter_loop (iter, "(qs(nn)nnq)", &sid,
  &sname, &avail, &mts, &vlm,  &clsid)) 
{
  //operate
}