GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Dec 19, 2014 11:59 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Memory Leak on GTK application
PostPosted: Fri May 30, 2014 7:49 am 
Offline
Familiar Face

Joined: Wed Jan 23, 2013 5:18 pm
Posts: 26
Hi,

I am using GTK+ 2.24.19

My application is running under powerpc linux.
I use Glade to create windows and widgets for my application.

There are 10 windows which shuffles on 5 seconds delay and each window has couple of label box and text box.
on every shuffle I use to call functions like gtk_widget_modify_fg ,gtk_widget_modify_font ,gtk_image_set_from_pixbuf , etc to change the color and size of font
and every thing is working well.

But when I run top command to see the usage of memory used by my application, I found that its is consistently increasing the memory usage.
also after couple of days I see my application exceeds the memory space and stops running.

I could see in every hour the memory usage is increased by 100 mb in RAM and when its reaches more the 100% it stops running.

here is the pmap results of my application , the last line shows the over all memory used in KB
Code:
00100000      12K r-xp  [vdso]
0e6dd000      12K r-xp  /opt/usr/local/lib/gdk-pixbuf-2.0/2.10.0/loaders/libpixbufloader-bmp.so
0e6e0000      64K ---p  /opt/usr/local/lib/gdk-pixbuf-2.0/2.10.0/loaders/libpixbufloader-bmp.so
0e6f0000       4K rwxp  /opt/usr/local/lib/gdk-pixbuf-2.0/2.10.0/loaders/libpixbufloader-bmp.so
0e701000       8K r-xp  /opt/usr/local/lib/pango/1.6.0/modules/pango-basic-fc.so
0e703000      60K ---p  /opt/usr/local/lib/pango/1.6.0/modules/pango-basic-fc.so
0e712000       4K rwxp  /opt/usr/local/lib/pango/1.6.0/modules/pango-basic-fc.so
0e723000      40K r-xp  /opt/usr/local/lib/directfb-1.6-0/wm/libdirectfbwm_default.so
0e72d000      60K ---p  /opt/usr/local/lib/directfb-1.6-0/wm/libdirectfbwm_default.so
0e73c000       4K rwxp  /opt/usr/local/lib/directfb-1.6-0/wm/libdirectfbwm_default.so
0e74d000      12K r-xp  /opt/usr/local/lib/directfb-1.6-0/inputdrivers/libdirectfb_keyboard.so
0e750000      60K ---p  /opt/usr/local/lib/directfb-1.6-0/inputdrivers/libdirectfb_keyboard.so
0e75f000       4K rwxp  /opt/usr/local/lib/directfb-1.6-0/inputdrivers/libdirectfb_keyboard.so
0e792000      20K r-xp  /opt/usr/local/lib/directfb-1.6-0/inputdrivers/libdirectfb_linux_input.so
0e797000      64K ---p  /opt/usr/local/lib/directfb-1.6-0/inputdrivers/libdirectfb_linux_input.so
0e7a7000       4K rwxp  /opt/usr/local/lib/directfb-1.6-0/inputdrivers/libdirectfb_linux_input.so
0e7db000      48K r-xp  /opt/usr/local/lib/directfb-1.6-0/systems/libdirectfb_fbdev.so
0e7e7000      64K ---p  /opt/usr/local/lib/directfb-1.6-0/systems/libdirectfb_fbdev.so
0e7f7000       4K rwxp  /opt/usr/local/lib/directfb-1.6-0/systems/libdirectfb_fbdev.so
0e84c000      36K r-xp  /lib/libnss_files.so.2
0e855000      60K ---p  /lib/libnss_files.so.2
0e864000       4K r--p  /lib/libnss_files.so.2
0e865000       4K rwxp  /lib/libnss_files.so.2
0e876000      36K r-xp  /lib/libnss_nis.so.2
0e87f000      60K ---p  /lib/libnss_nis.so.2
0e88e000       4K r--p  /lib/libnss_nis.so.2
0e88f000       4K rwxp  /lib/libnss_nis.so.2
0e8a0000      64K r-xp  /lib/libnsl.so.1
0e8b0000      60K ---p  /lib/libnsl.so.1
0e8bf000       4K r--p  /lib/libnsl.so.1
0e8c0000       4K rwxp  /lib/libnsl.so.1
0e8c1000       8K rwxp    [ anon ]
0e8d3000      24K r-xp  /lib/libnss_compat.so.2
0e8d9000      60K ---p  /lib/libnss_compat.so.2
0e8e8000       4K r--p  /lib/libnss_compat.so.2
0e8e9000       4K rwxp  /lib/libnss_compat.so.2
0e8fa000      60K r-xp  /lib/libresolv.so.2
0e909000      60K ---p  /lib/libresolv.so.2
0e918000       4K r--p  /lib/libresolv.so.2
0e919000       4K rwxp  /lib/libresolv.so.2
0e91a000       8K rwxp    [ anon ]
0e92c000    1172K r-xp  /lib/libc.so.6
0ea51000      60K ---p  /lib/libc.so.6
0ea60000       8K r--p  /lib/libc.so.6
0ea62000      12K rwxp  /lib/libc.so.6
0ea65000      12K rwxp    [ anon ]
0ea78000      80K r-xp  /lib/libgcc_s.so.1
0ea8c000      64K ---p  /lib/libgcc_s.so.1
0ea9c000       4K rwxp  /lib/libgcc_s.so.1
0eaad000    1336K r-xp  /lib/libstdc++.so.6
0ebfb000      60K ---p  /lib/libstdc++.so.6
0ec0a000      20K r--p  /lib/libstdc++.so.6
0ec0f000       4K rwxp  /lib/libstdc++.so.6
0ec10000      44K rwxp    [ anon ]
0ec2b000    1368K r-xp  /usr/lib/libgio-2.0.so.0.3400.0
0ed81000      64K ---p  /usr/lib/libgio-2.0.so.0.3400.0
0ed91000      36K rwxp  /usr/lib/libgio-2.0.so.0.3400.0
0ed9a000      12K rwxp    [ anon ]
0edad000     572K r-xp  /usr/lib/libgdk-directfb-2.0.so.0.2400.19
0ee3c000      60K ---p  /usr/lib/libgdk-directfb-2.0.so.0.2400.19
0ee4b000      32K rwxp  /usr/lib/libgdk-directfb-2.0.so.0.2400.19
0ee53000       4K rwxp    [ anon ]
0ee64000    4524K r-xp  /usr/lib/libgtk-directfb-2.0.so.0.2400.19
0f2cf000      60K ---p  /usr/lib/libgtk-directfb-2.0.so.0.2400.19
0f2de000      80K rwxp  /usr/lib/libgtk-directfb-2.0.so.0.2400.19
0f2f2000      24K rwxp    [ anon ]
0f308000    1048K r-xp  /lib/libcrypto.so.1.0.0
0f40e000      64K ---p  /lib/libcrypto.so.1.0.0
0f41e000      72K rwxp  /lib/libcrypto.so.1.0.0
0f430000      12K rwxp    [ anon ]
0f443000     492K r-xp  /lib/libsqlite3.so.0.8.6
0f4be000      60K ---p  /lib/libsqlite3.so.0.8.6
0f4cd000      12K rwxp  /lib/libsqlite3.so.0.8.6
0f4d0000       4K rwxp    [ anon ]
0f4e1000      72K r-xp  /lib/libcds++.so
0f4f3000      64K ---p  /lib/libcds++.so
0f503000       4K rwxp  /lib/libcds++.so
0f514000     380K r-xp  /lib/libcds.so
0f573000      64K ---p  /lib/libcds.so
0f583000       4K rwxp  /lib/libcds.so
0f594000     604K r-xp  /lib/libm.so.6
0f62b000      60K ---p  /lib/libm.so.6
0f63a000       4K r--p  /lib/libm.so.6
0f63b000      12K rwxp  /lib/libm.so.6
0f64e000      28K r-xp  /usr/lib/libffi.so.5.0.1
0f655000      64K ---p  /usr/lib/libffi.so.5.0.1
0f665000       4K rwxp  /usr/lib/libffi.so.5.0.1
0f676000      24K r-xp  /lib/librt.so.1
0f67c000      60K ---p  /lib/librt.so.1
0f68b000       4K r--p  /lib/librt.so.1
0f68c000       4K rwxp  /lib/librt.so.1
0f69d000     120K r-xp  /usr/lib/libgdk_pixbuf-2.0.so.0.2600.5
0f6bb000      60K ---p  /usr/lib/libgdk_pixbuf-2.0.so.0.2600.5
0f6ca000       8K rwxp  /usr/lib/libgdk_pixbuf-2.0.so.0.2600.5
0f6dc000     484K r-xp  /usr/lib/libfreetype.so.6.5.0
0f755000      60K ---p  /usr/lib/libfreetype.so.6.5.0
0f764000      16K rwxp  /usr/lib/libfreetype.so.6.5.0
0f778000     212K r-xp  /usr/lib/libfontconfig.so.1.6.2
0f7ad000      64K ---p  /usr/lib/libfontconfig.so.1.6.2
0f7bd000       8K rwxp  /usr/lib/libfontconfig.so.1.6.2
0f7cf000     308K r-xp  /usr/lib/libpixman-1.so.0.28.2
0f81c000      64K ---p  /usr/lib/libpixman-1.so.0.28.2
0f82c000      12K rwxp  /usr/lib/libpixman-1.so.0.28.2
0f83f000      72K r-xp  /lib/libz.so.1.2.5
0f851000      60K ---p  /lib/libz.so.1.2.5
0f860000       4K rwxp  /lib/libz.so.1.2.5
0f871000     152K r-xp  /usr/lib/libpng15.so.15.13.0
0f897000      64K ---p  /usr/lib/libpng15.so.15.13.0
0f8a7000       4K rwxp  /usr/lib/libpng15.so.15.13.0
0f8b8000       4K r-xp  /usr/lib/libgthread-2.0.so.0.3400.0
0f8b9000      60K ---p  /usr/lib/libgthread-2.0.so.0.3400.0
0f8c8000       4K rwxp  /usr/lib/libgthread-2.0.so.0.3400.0
0f8d9000     608K r-xp  /usr/lib/libcairo.so.2.11000.0
0f971000      64K ---p  /usr/lib/libcairo.so.2.11000.0
0f981000       8K rwxp  /usr/lib/libcairo.so.2.11000.0
0f983000      12K rwxp    [ anon ]
0f996000    1172K r-xp  /usr/lib/libglib-2.0.so.0.3400.0
0fabb000      64K ---p  /usr/lib/libglib-2.0.so.0.3400.0
0facb000      12K rwxp  /usr/lib/libglib-2.0.so.0.3400.0
0face000       4K rwxp    [ anon ]
0fadf000       8K r-xp  /lib/libdl.so.2
0fae1000      60K ---p  /lib/libdl.so.2
0faf0000       4K r--p  /lib/libdl.so.2
0faf1000       4K rwxp  /lib/libdl.so.2
0fb02000      12K r-xp  /usr/lib/libgmodule-2.0.so.0.3400.0
0fb05000      60K ---p  /usr/lib/libgmodule-2.0.so.0.3400.0
0fb14000       4K rwxp  /usr/lib/libgmodule-2.0.so.0.3400.0
0fb25000     308K r-xp  /usr/lib/libgobject-2.0.so.0.3400.0
0fb72000      60K ---p  /usr/lib/libgobject-2.0.so.0.3400.0
0fb81000       8K rwxp  /usr/lib/libgobject-2.0.so.0.3400.0
0fb83000       4K rwxp    [ anon ]
0fb94000     124K r-xp  /usr/lib/libatk-1.0.so.0.20609.1
0fbb3000      60K ---p  /usr/lib/libatk-1.0.so.0.20609.1
0fbc2000      12K rwxp  /usr/lib/libatk-1.0.so.0.20609.1
0fbd5000    1268K r-xp  /usr/lib/libxml2.so.2.9.0
0fd12000      60K ---p  /usr/lib/libxml2.so.2.9.0
0fd21000      36K rwxp  /usr/lib/libxml2.so.2.9.0
0fd2a000       4K rwxp    [ anon ]
0fd3b000     120K r-xp  /usr/lib/libdirect-1.6.so.0.3.0
0fd59000      64K ---p  /usr/lib/libdirect-1.6.so.0.3.0
0fd69000       4K rwxp  /usr/lib/libdirect-1.6.so.0.3.0
0fd6a000       4K rwxp    [ anon ]
0fd7b000      48K r-xp  /usr/lib/libfusion-1.6.so.0.3.0
0fd87000      60K ---p  /usr/lib/libfusion-1.6.so.0.3.0
0fd96000       4K rwxp  /usr/lib/libfusion-1.6.so.0.3.0
0fda7000    1132K r-xp  /usr/lib/libdirectfb-1.6.so.0.3.0
0fec2000      60K ---p  /usr/lib/libdirectfb-1.6.so.0.3.0
0fed1000      32K rwxp  /usr/lib/libdirectfb-1.6.so.0.3.0
0fed9000      16K rwxp    [ anon ]
0feed000     276K r-xp  /usr/lib/libpango-1.0.so.0.3000.0
0ff32000      64K ---p  /usr/lib/libpango-1.0.so.0.3000.0
0ff42000       8K rwxp  /usr/lib/libpango-1.0.so.0.3000.0
0ff44000       4K rwxp    [ anon ]
0ff55000     160K r-xp  /usr/lib/libpangoft2-1.0.so.0.3000.0
0ff7d000      60K ---p  /usr/lib/libpangoft2-1.0.so.0.3000.0
0ff8c000       8K rwxp  /usr/lib/libpangoft2-1.0.so.0.3000.0
0ff9e000      40K r-xp  /usr/lib/libpangocairo-1.0.so.0.3000.0
0ffa8000      64K ---p  /usr/lib/libpangocairo-1.0.so.0.3000.0
0ffb8000       4K rwxp  /usr/lib/libpangocairo-1.0.so.0.3000.0
0ffc9000      80K r-xp  /lib/libpthread.so.0
0ffdd000      60K ---p  /lib/libpthread.so.0
0ffec000       4K r--p  /lib/libpthread.so.0
0ffed000       4K rwxp  /lib/libpthread.so.0
0ffee000       8K rwxp    [ anon ]
10000000     244K r-xp  /bin/myapp
1004d000       8K rwxp  /bin/myapp
1004f000   14536K rwxp  [heap]
48000000     108K r-xp  /lib/ld.so.1
4801b000      48K rw-p    [ anon ]
4802a000       4K r--p  /lib/ld.so.1
4802b000       8K rwxp  /lib/ld.so.1
4802d000    2048K rw-s  /dev/shm/libipc.2107.0
4822d000    4096K rw-s  /dev/shm/libipc.2107.0
4862d000     256K rw-s  /dev/Jemlcd
4866d000     256K rw-s  /dev/fb0
486ad000       4K ---p    [ anon ]
486ae000    8188K rwxp    [ anon ]
48ead000       4K ---p    [ anon ]
48eae000    8188K rwxp    [ anon ]
496ad000       4K ---p    [ anon ]
496ae000    8188K rwxp    [ anon ]
49ead000       4K ---p    [ anon ]
49eae000    8188K rwxp    [ anon ]
4a6ad000     256K rw-s  /dev/fb0
4a6ed000       4K ---p    [ anon ]
4a6ee000    8188K rwxp    [ anon ]
4aeed000       4K ---p    [ anon ]
4aeee000    8188K rwxp    [ anon ]
4b6ed000       4K ---p    [ anon ]
4b6ee000    8188K rwxp    [ anon ]
bfe01000      84K rwxp  [stack]
mapped: 101620K



Here is the sample code which creates the memory leak, the function Update_display will set the font attributes which makes the memory leak
Code:
GtkWidget *screens;      //single screen
GtkLabel *label_txt;   //label inside screen
GdkColor white;         //font color of label
int main( int argc, char **argv )
{
   GError *error = NULL;

   //Make GTK thread safe
   gdk_threads_init ();
   gdk_threads_enter ();
   
   //Initialise GTK+
   gtk_init( &argc, &argv );

   //Create new GtkBuilder object
   builder = gtk_builder_new();

   //Load UI from file. If error occurs, report it and quit application.
   if( ! gtk_builder_add_from_file( builder, "myapp.glade", &error ) )
   {
      g_warning( "%s", error->message );
      g_free( error );
      return( 1 );
   }
   
   
   //color for text
   RgbColorInsert (65535, 65535, 65535, &white);
   
   //set label font size and color
   label_txt = GTK_LABEL( gtk_builder_get_object( builder, <label_name /*label name in glade file*/> ) );
   gtk_widget_modify_font(GTK_WIDGET(label_txt), pango_font_description_from_string("Vera 10"));
    gtk_widget_modify_fg (GTK_WIDGET(label_txt), GTK_STATE_NORMAL, &white);
   
   //Connect the main screens to the callback functions
   g_signal_connect (G_OBJECT (screens), "show",
      G_CALLBACK (Show_screen_callback), NULL);
   
   g_signal_connect(screens, "destroy", G_CALLBACK (gtk_main_quit), NULL);


   //Connect signals
   gtk_builder_connect_signals( builder, NULL );

   //Destroy builder, since we don't need it anymore
   g_object_unref( G_OBJECT( builder ) );

   
   //100 mili sec gtk thread
//Update_display will get called every 100 mili sec
   g_timeout_add(100, Update_display, screens);

   //Start main loop
   gtk_main();

   //thread safe release
   gdk_threads_leave ();

   return( 0 );
}
//
* DESCRIPTION: this function will just set the font color and font size on every call
* its just a test call to prove the memory leak
//
gboolean Update_display(gpointer widget)
{
    gtk_widget_modify_fg (GTK_WIDGET(label_txt), GTK_STATE_NORMAL, &white);
    gtk_widget_modify_font(GTK_WIDGET(label_txt), pango_font_description_from_string("Vera 8"));   
return TRUE;
}
//
*  DESCRIPTION: this function creates a GtkColor from the red, green, and blue attributes that the
*  calling function passes in.
//
void RgbColorInsert (int red, int green, int blue, GdkColor *pColor)
{
    pColor->red = red;
    pColor->green = green;
    pColor->blue = blue;
}


Please help me to find what is going wrong here.


Thanks in advance


Top
 Profile  
 
 Post subject: Re: Memory Leak on GTK application
PostPosted: Fri May 30, 2014 7:14 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 799
Location: UK
Hello,

The memory leak is in the use of the function pango_font_description_from_string(). This API function allocates a PangoFontDescription which must be freed.

So your section of code that does the updates should be as follows
Code:
gboolean Update_display(gpointer widget)
{
    PangoFontDescription *fd = pango_font_description_from_string("Vera 8")
    gtk_widget_modify_fg (GTK_WIDGET(label_txt), GTK_STATE_NORMAL, &white);
    gtk_widget_modify_font(GTK_WIDGET(label_txt), fd);
    pango_font_description_free (fd);
    return TRUE;
}

Alternatively you can preallocate the PangoFontDescription's to save allocating then freeing every display update and then free them on application exit.

By the way there is another memory leak in your test code based on the same error.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Memory Leak on GTK application
PostPosted: Mon Jun 02, 2014 6:40 am 
Offline
Familiar Face

Joined: Wed Jan 23, 2013 5:18 pm
Posts: 26
Hi Errol,

That was a excellent catch, Thanks for your prompt response.

Yes you are right pango_font_description_from_string() must be freed.

I changed my code and now I see the memory usage is not increased by font related calls.

how ever I see still some more memory leak in my code, with the above reference I am checking all other gtk API for memory leak.
I will fix it soon.

I have 2 Questions , please advice

1. Is there is any way to find the API which leaks memory like pango_font_description_from_string(), I understood reading API document will help but still any other way?
2. My gtk application is taking all the time 41 % of memory , Is that the way GTK usually occupy the memory?, if no than what could be the reason?(sample app also occupying 40 % of memory)

Thanks once again.


Top
 Profile  
 
 Post subject: Re: Memory Leak on GTK application
PostPosted: Wed Jun 04, 2014 4:47 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 799
Location: UK
There are many tools out there that can help with debugging memory problems.

First there are static analysis tools that look at your source code for potential errors. Some tools are
- lint
- flawfinder http://www.dwheeler.com/flawfinder/
- rats

These can find some errors, but can give you some false positives.

- duma http://duma.sourceforge.net/
- electric-fence
- The built-in Glib memory profiling https://developer.gnome.org/glib/stable/glib-Memory-Allocation.html https://developer.gnome.org/glib/stable/glib-running.html

These work with the processor or intercept the memory allocations/memory access to give you a profile or break on an invalid memory access.

Finally there is Valgrind http://www.valgrind.org/. This is very powerful as it emulates the processor, this does mean it is very slow.

With the memory tools there would appear to be various memory leaks. This is OK as they may be buffers that is needed for various libraries which are allocated on initialisation and are often needed for the life time of the application.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Memory Leak on GTK application
PostPosted: Tue Jun 17, 2014 8:57 am 
Offline
Familiar Face

Joined: Wed Jan 23, 2013 5:18 pm
Posts: 26
Hi Errol,

Finally I could compile Valgrind with my build.
Thanks for the guidance , I will let you know how is going.

_________________
Thanks,
Arief.
None of us is stronger than all of us


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group