Re: [GLIB] memory leak in glib 2.32.4 ?

Hi Arthur,

In C code, memory is managed manually. Every time you dynamically allocate memory, such as with GLib's various _new() functions, you need to return that chunk of memory to the system when you are done using it. I see no such calls in your code. For example, a GPtrArray created with g_ptr_array_new() should have a matching call to g_ptr_array_unref() or g_ptr_array_free(). A call to g_new0 should have a matching g_free, I don't know why you have commented that out. And so on.

Best regards! Simon

On Tue, Mar 4, 2014 at 3:44 PM, Arthur Lambert <lambertarthur22 gmail com> wrote:

Hi All,

I am using glib with GValue, GPtrArray to communicate with dbus. I
have a big memory leak in my code. I am running an embedded target
with glib 2.32.4. I am currently trying to  code some little binary to
detect the leak. I am not the author of the code so it is quite hard
for me to debug it.

So I have currently for example a little main which does not leak at
all on a desktop linux version (Fedora 17) which use the same glib
version( perhaps not exactly the same verson but major, minor and
macro number version seems to be the same. But when I run this piece
of code on my embedded linux, I have a leak of 4 Bytes per loop

Code is :

static void data_constructor_array_struct_string_array_struct_string_variant(GPtrArray
*data, const gchar *client_objname)
  guint member_count;
  GValueArray *outer_struct = g_value_array_new(1);
  GPtrArray *inner_array = g_ptr_array_new ();

  printf ("client objname : %s\n", client_objname);

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

  for(member_count=0;member_count < SIZE_INNER_ARRAY ;member_count++)
    GValueArray *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);

    char str[32];
    sprintf (str, "TUTU%d", member_count);
    g_value_set_string(g_value_array_get_nth(inner_struct, 0), str);

    GValue *ptr = g_new0 (GValue, 1);
    g_value_init (ptr, G_TYPE_UINT);
    g_value_set_uint (ptr, member_count);

    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), ptr);
    //g_free(ptr); // can remove it, but use set_static instead of set

    g_ptr_array_add(inner_array, inner_struct);

  g_value_array_append(outer_struct, NULL);
  g_value_init(g_value_array_get_nth(outer_struct, 1),
  g_value_set_boxed (g_value_array_get_nth(outer_struct, 1), inner_array);

  g_ptr_array_add(data, outer_struct);

int main (void)
  GPtrArray *ptr_array = NULL;
  g_type_init ();
  //while (1)
    ptr_array = g_ptr_array_new ();
(ptr_array, "mon objet");
    //sleep (3);
  return 0;

To summarize the code. I have an outer_struct which is a GValueArray.
First element is a gvalue with a string. Second element is inner_array
which is a GPtrArray. Each element of inner_array is a innner_struct
which is a GValueArray compose of two elements. As you can see in this
example I am adding 5 inner_struct in the inner_array.

Do I am doing something wrong in this code ? Do I have a memory leak
in my glib version ?
I hope that it is the good place to ask my request.

Thanks & Regards
gnome-devel-list mailing list
gnome-devel-list gnome org

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]