ArthurLambert
ArthurLambert

Reputation: 793

Using properly GPtrArray and GValueArray with glib dbus

I am using glib with GValue, GPtrArray and GValueArray to communicate with dbus. I have a big memory leak in my code. I am running an embedded target with glib 2.32.4. It is the first time that I use glib type in a code so I am not very familiar with all theses structures.

Message definition is :

   <method name="GetAll">
        <arg name="in" type="as" direction="in"/>
        <arg name="out" type="a(sa(sv))" direction="out"/>
    </method>

Code is :

void data_constructor_array_struct_string_array_struct_string_variant (GPtrArray* out_data, const gchar *client_objname)
{
  guint member_count;
  GValueArray *outer_struct = g_value_array_new(3);
  GPtrArray *inner_array = g_ptr_array_new ();
  GValueArray *inner_struct = NULL;

  g_value_array_append(outer_struct, NULL);
  g_value_init(g_value_array_get_nth(outer_struct, 0), G_TYPE_STRING);
  g_value_set_static_string(g_value_array_get_nth(outer_struct, 0), client_objname);

  for(member_count=0;param->members[member_count].members_name;member_count++)
  {
    if(param->members[member_count].Isneeded)
    {
      log_warning("%s member added %s\n", __FUNCTION__, param->members[member_count].members_name);
      inner_struct = g_value_array_new(2);
      g_value_array_append(inner_struct, NULL);
      g_value_init(g_value_array_get_nth(inner_struct, 0), G_TYPE_STRING);
      g_value_set_static_string(g_value_array_get_nth(inner_struct, 0), param->members[member_count].members_name);

      g_value_array_append(inner_struct, NULL);
      g_value_init(g_value_array_get_nth(inner_struct, 1), G_TYPE_VALUE);
      g_value_set_static_boxed(g_value_array_get_nth(inner_struct, 1), param->members[member_count].data);

      g_value_array_append(inner_struct, NULL);
      g_ptr_array_add(inner_array, inner_struct);
    }
  }

  //g_ptr_array_add(inner_array, NULL); // CRASH

  g_value_array_append(outer_struct, NULL);
  g_value_init(g_value_array_get_nth(outer_struct, 1),  dbus_g_type_get_collection("GPtrArray",dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING, G_TYPE_VALUE, G_TYPE_INVALID)));
  g_value_set_boxed (g_value_array_get_nth(outer_struct, 1), inner_array);

  g_value_array_append(outer_struct, NULL);
  g_ptr_array_add(out_data, outer_struct);
  //g_ptr_array_add(out_data, NULL); //CRASH
  g_ptr_array_free (inner_array, TRUE);
}

I tried to add some NULL in the upper structure which is out_data. But if I try to add NULL to the end of the data I got this at runtime :

(process:1568): GLib-GObject-CRITICAL **: g_value_array_get_nth: assertion `index < value_array->n_values' failed

(process:1568): GLib-GObject-CRITICAL **: g_value_copy: assertion `G_IS_VALUE (src_value)' failed

(process:1568): GLib-GObject-CRITICAL **: g_value_array_get_nth: assertion `index < value_array->n_values' failed

(process:1568): GLib-GObject-CRITICAL **: g_value_copy: assertion `G_IS_VALUE (src_value)' failed

glib dbus must free all my structure by itself but in my case, it does not work... I tried also to run valgrind but it does not see anything...

Thanks & Regards Arthur.

Upvotes: 3

Views: 1158

Answers (1)

Jabberwocky
Jabberwocky

Reputation: 50831

For every g_value_array_new you must call g_value_array_free. Just as for every new you must have a corresponding delete.

Upvotes: 2

Related Questions