Re: [GLIB] memory leak in glib 2.32.4 ?
Simon Kågedal Reimer <[email protected]> Thu, 6 Mar 2014 12:58:57 +0100
| Newsgroups | gmane.comp.gnome.devel |
|---|---|
| Message-ID | <CAP5gDJXhXydn8zKcgAVKVDhLexX3Fceh0qfJC6u-F=S-RFQDwQ@mail.gmail.com> |
--===============4135807175698013728== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline --===============4135807175698013728== Content-Type: multipart/alternative; boundary=089e012940d8b18f4b04f3eedc22 --089e012940d8b18f4b04f3eedc22 Content-Type: text/plain; charset=ISO-8859-1 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 <[email protected]>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 > iteration. > > Code is : > #define SIZE_INNER_ARRAY 5 > ... > > 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), > 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_ptr_array_add(data, outer_struct); > } > > int main (void) > { > GPtrArray *ptr_array = NULL; > g_type_init (); > //while (1) > { > ptr_array = g_ptr_array_new (); > data_constructor_array_struct_string_array_struct_string_variant > (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 > Arthur. > _______________________________________________ > gnome-devel-list mailing list > [email protected] > https://mail.gnome.org/mailman/listinfo/gnome-devel-list > --089e012940d8b18f4b04f3eedc22 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div><div>Hi Arthur,<br><br></div>In C code, memory is man= aged manually. Every time you dynamically allocate memory, such as with GLi= b's various _new() functions, you need to return that chunk of memory t= o 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 match= ing call to g_ptr_array_unref() or g_ptr_array_free(). A call to g_new0 sho= uld have a matching g_free, I don't know why you have commented that ou= t. And so on.<br> <br></div>Best regards! Simon <br><div><div class=3D"gmail_extra"><br><br><= div class=3D"gmail_quote">On Tue, Mar 4, 2014 at 3:44 PM, Arthur Lambert <s= pan dir=3D"ltr"><<a href=3D"mailto:[email protected]" target=3D"= _blank">[email protected]</a>></span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"><br> Hi All,<br> <br> I am using glib with GValue, GPtrArray to communicate with dbus. I<br> have a big memory leak in my code. I am running an embedded target<br> with glib 2.32.4. I am currently trying to =A0code some little binary to<br= > detect the leak. I am not the author of the code so it is quite hard<br> for me to debug it.<br> <br> So I have currently for example a little main which does not leak at<br> all on a desktop linux version (Fedora 17) which use the same glib<br> version( perhaps not exactly the same verson but major, minor and<br> macro number version seems to be the same. But when I run this piece<br> of code on my embedded linux, I have a leak of 4 Bytes per loop<br> iteration.<br> <br> Code is :<br> #define SIZE_INNER_ARRAY 5<br> ...<br> <br> static void data_constructor_array_struct_string_array_struct_string_varian= t(GPtrArray<br> *data, const gchar *client_objname)<br> {<br> =A0 guint member_count;<br> =A0 GValueArray *outer_struct =3D g_value_array_new(1);<br> =A0 GPtrArray *inner_array =3D g_ptr_array_new ();<br> <br> =A0 printf ("client objname : %s\n", client_objname);<br> <br> =A0 g_value_array_append(outer_struct, NULL);<br> =A0 g_value_init(g_value_array_get_nth(outer_struct, 0), G_TYPE_STRING);<br= > =A0 g_value_set_string(g_value_array_get_nth(outer_struct, 0), client_objna= me);<br> <br> =A0 for(member_count=3D0;member_count < SIZE_INNER_ARRAY ;member_count++= )<br> =A0 {<br> =A0 =A0 GValueArray *inner_struct =3D g_value_array_new(2);<br> =A0 =A0 g_value_array_append(inner_struct, NULL);<br> =A0 =A0 g_value_init(g_value_array_get_nth(inner_struct, 0), G_TYPE_STRING)= ;<br> <br> =A0 =A0 char str[32];<br> =A0 =A0 sprintf (str, "TUTU%d", member_count);<br> =A0 =A0 g_value_set_string(g_value_array_get_nth(inner_struct, 0), str);<br= > <br> =A0 =A0 GValue *ptr =3D g_new0 (GValue, 1);<br> =A0 =A0 g_value_init (ptr, G_TYPE_UINT);<br> =A0 =A0 g_value_set_uint (ptr, member_count);<br> <br> =A0 =A0 g_value_array_append(inner_struct, NULL);<br> =A0 =A0 g_value_init(g_value_array_get_nth(inner_struct, 1), G_TYPE_VALUE);= <br> =A0 =A0 g_value_set_static_boxed(g_value_array_get_nth(inner_struct, 1), pt= r);<br> =A0 =A0 //g_free(ptr); // can remove it, but use set_static instead of set<= br> <br> =A0 =A0 g_ptr_array_add(inner_array, inner_struct);<br> =A0 }<br> <br> =A0 g_value_array_append(outer_struct, NULL);<br> =A0 g_value_init(g_value_array_get_nth(outer_struct, 1),<br> dbus_g_type_get_collection("GPtrArray",dbus_g_type_get_struct<br> ("GValueArray", G_TYPE_STRING, G_TYPE_VALUE, G_TYPE_INVALID)));<b= r> =A0 g_value_set_boxed (g_value_array_get_nth(outer_struct, 1), inner_array)= ;<br> <br> =A0 g_ptr_array_add(data, outer_struct);<br> }<br> <br> int main (void)<br> {<br> =A0 GPtrArray *ptr_array =3D NULL;<br> =A0 g_type_init ();<br> =A0 //while (1)<br> =A0 {<br> =A0 =A0 ptr_array =3D g_ptr_array_new ();<br> =A0 =A0 data_constructor_array_struct_string_array_struct_string_variant<br= > (ptr_array, "mon objet");<br> =A0 =A0 //sleep (3);<br> =A0 }<br> =A0 return 0;<br> }<br> <br> To summarize the code. I have an outer_struct which is a GValueArray.<br> First element is a gvalue with a string. Second element is inner_array<br> which is a GPtrArray. Each element of inner_array is a innner_struct<br> which is a GValueArray compose of two elements. As you can see in this<br> example I am adding 5 inner_struct in the inner_array.<br> <br> Do I am doing something wrong in this code ? Do I have a memory leak<br> in my glib version ?<br> I hope that it is the good place to ask my request.<br> <br> Thanks & Regards<br> Arthur.<br> _______________________________________________<br> gnome-devel-list mailing list<br> <a href=3D"mailto:[email protected]">[email protected]</a= ><br> <a href=3D"https://mail.gnome.org/mailman/listinfo/gnome-devel-list" target= =3D"_blank">https://mail.gnome.org/mailman/listinfo/gnome-devel-list</a><br= > </blockquote></div><br></div></div></div> --089e012940d8b18f4b04f3eedc22-- --===============4135807175698013728== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ gnome-devel-list mailing list [email protected] https://mail.gnome.org/mailman/listinfo/gnome-devel-list --===============4135807175698013728==--