我正在使用glib与GValue,GPtrArray和GValueArray与dbus进行通信。我的代码中有很大的内存泄漏。我正在使用glib 2.32.4运行嵌入式目标。这是我第一次在代码中使用glib类型,所以我不太熟悉所有这些结构。
消息定义是:
<method name="GetAll">
<arg name="in" type="as" direction="in"/>
<arg name="out" type="a(sa(sv))" direction="out"/>
</method>
代码是:
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);
}
我试图在上层结构中添加一些NULL,这是out_data。但是如果我尝试在数据的末尾添加NULL,我在运行时得到了这个:
(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必须自己释放我的所有结构,但在我的情况下,它不起作用...我也尝试运行valgrind但它没有看到任何东西......
谢谢&amp;问候 亚瑟。
答案 0 :(得分:2)
对于每个g_value_array_new
,您必须致电g_value_array_free
。与每个new
一样,您必须拥有相应的delete
。