Search code examples
cglibdbus

How to safely use g_variant_iter_loop with data received via DBUS


My stack trace looks like this

/usr/lib/libglib-2.0.so.0(g_variant_iter_loop+0xb4)
/usr/lib/libglib-2.0.so.0(g_variant_get_int32+0x1c) 
/usr/lib/libglib-2.0.so.0(g_variant_type_is_subtype_of+0x3c) 

and the code for it is like:

while ((NULL != iterator) && (true == g_variant_iter_loop (iterator, "y", &extractedValue)))
{
  // do something with extractedValue
}

The data is received via DBUS using a GVariant. Maybe i'm not using glib API properly, but this fails only like 1% of the cases. Am i doing something wrong, or i am missing something ?


Solution

  • When only one element is expected, is better to use

    if(iter != NULL) {
        g_variant_iter_next (iterator, "u", &value);
    }