GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Aug 31, 2016 2:18 pm

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 
Familiar Face

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

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
00100000      12K r-xp  [vdso]
0e6dd000      12K r-xp  /opt/usr/local/lib/gdk-pixbuf-2.0/2.10.0/loaders/
0e6e0000      64K ---p  /opt/usr/local/lib/gdk-pixbuf-2.0/2.10.0/loaders/
0e6f0000       4K rwxp  /opt/usr/local/lib/gdk-pixbuf-2.0/2.10.0/loaders/
0e701000       8K r-xp  /opt/usr/local/lib/pango/1.6.0/modules/
0e703000      60K ---p  /opt/usr/local/lib/pango/1.6.0/modules/
0e712000       4K rwxp  /opt/usr/local/lib/pango/1.6.0/modules/
0e723000      40K r-xp  /opt/usr/local/lib/directfb-1.6-0/wm/
0e72d000      60K ---p  /opt/usr/local/lib/directfb-1.6-0/wm/
0e73c000       4K rwxp  /opt/usr/local/lib/directfb-1.6-0/wm/
0e74d000      12K r-xp  /opt/usr/local/lib/directfb-1.6-0/inputdrivers/
0e750000      60K ---p  /opt/usr/local/lib/directfb-1.6-0/inputdrivers/
0e75f000       4K rwxp  /opt/usr/local/lib/directfb-1.6-0/inputdrivers/
0e792000      20K r-xp  /opt/usr/local/lib/directfb-1.6-0/inputdrivers/
0e797000      64K ---p  /opt/usr/local/lib/directfb-1.6-0/inputdrivers/
0e7a7000       4K rwxp  /opt/usr/local/lib/directfb-1.6-0/inputdrivers/
0e7db000      48K r-xp  /opt/usr/local/lib/directfb-1.6-0/systems/
0e7e7000      64K ---p  /opt/usr/local/lib/directfb-1.6-0/systems/
0e7f7000       4K rwxp  /opt/usr/local/lib/directfb-1.6-0/systems/
0e84c000      36K r-xp  /lib/
0e855000      60K ---p  /lib/
0e864000       4K r--p  /lib/
0e865000       4K rwxp  /lib/
0e876000      36K r-xp  /lib/
0e87f000      60K ---p  /lib/
0e88e000       4K r--p  /lib/
0e88f000       4K rwxp  /lib/
0e8a0000      64K r-xp  /lib/
0e8b0000      60K ---p  /lib/
0e8bf000       4K r--p  /lib/
0e8c0000       4K rwxp  /lib/
0e8c1000       8K rwxp    [ anon ]
0e8d3000      24K r-xp  /lib/
0e8d9000      60K ---p  /lib/
0e8e8000       4K r--p  /lib/
0e8e9000       4K rwxp  /lib/
0e8fa000      60K r-xp  /lib/
0e909000      60K ---p  /lib/
0e918000       4K r--p  /lib/
0e919000       4K rwxp  /lib/
0e91a000       8K rwxp    [ anon ]
0e92c000    1172K r-xp  /lib/
0ea51000      60K ---p  /lib/
0ea60000       8K r--p  /lib/
0ea62000      12K rwxp  /lib/
0ea65000      12K rwxp    [ anon ]
0ea78000      80K r-xp  /lib/
0ea8c000      64K ---p  /lib/
0ea9c000       4K rwxp  /lib/
0eaad000    1336K r-xp  /lib/
0ebfb000      60K ---p  /lib/
0ec0a000      20K r--p  /lib/
0ec0f000       4K rwxp  /lib/
0ec10000      44K rwxp    [ anon ]
0ec2b000    1368K r-xp  /usr/lib/
0ed81000      64K ---p  /usr/lib/
0ed91000      36K rwxp  /usr/lib/
0ed9a000      12K rwxp    [ anon ]
0edad000     572K r-xp  /usr/lib/
0ee3c000      60K ---p  /usr/lib/
0ee4b000      32K rwxp  /usr/lib/
0ee53000       4K rwxp    [ anon ]
0ee64000    4524K r-xp  /usr/lib/
0f2cf000      60K ---p  /usr/lib/
0f2de000      80K rwxp  /usr/lib/
0f2f2000      24K rwxp    [ anon ]
0f308000    1048K r-xp  /lib/
0f40e000      64K ---p  /lib/
0f41e000      72K rwxp  /lib/
0f430000      12K rwxp    [ anon ]
0f443000     492K r-xp  /lib/
0f4be000      60K ---p  /lib/
0f4cd000      12K rwxp  /lib/
0f4d0000       4K rwxp    [ anon ]
0f4e1000      72K r-xp  /lib/
0f4f3000      64K ---p  /lib/
0f503000       4K rwxp  /lib/
0f514000     380K r-xp  /lib/
0f573000      64K ---p  /lib/
0f583000       4K rwxp  /lib/
0f594000     604K r-xp  /lib/
0f62b000      60K ---p  /lib/
0f63a000       4K r--p  /lib/
0f63b000      12K rwxp  /lib/
0f64e000      28K r-xp  /usr/lib/
0f655000      64K ---p  /usr/lib/
0f665000       4K rwxp  /usr/lib/
0f676000      24K r-xp  /lib/
0f67c000      60K ---p  /lib/
0f68b000       4K r--p  /lib/
0f68c000       4K rwxp  /lib/
0f69d000     120K r-xp  /usr/lib/
0f6bb000      60K ---p  /usr/lib/
0f6ca000       8K rwxp  /usr/lib/
0f6dc000     484K r-xp  /usr/lib/
0f755000      60K ---p  /usr/lib/
0f764000      16K rwxp  /usr/lib/
0f778000     212K r-xp  /usr/lib/
0f7ad000      64K ---p  /usr/lib/
0f7bd000       8K rwxp  /usr/lib/
0f7cf000     308K r-xp  /usr/lib/
0f81c000      64K ---p  /usr/lib/
0f82c000      12K rwxp  /usr/lib/
0f83f000      72K r-xp  /lib/
0f851000      60K ---p  /lib/
0f860000       4K rwxp  /lib/
0f871000     152K r-xp  /usr/lib/
0f897000      64K ---p  /usr/lib/
0f8a7000       4K rwxp  /usr/lib/
0f8b8000       4K r-xp  /usr/lib/
0f8b9000      60K ---p  /usr/lib/
0f8c8000       4K rwxp  /usr/lib/
0f8d9000     608K r-xp  /usr/lib/
0f971000      64K ---p  /usr/lib/
0f981000       8K rwxp  /usr/lib/
0f983000      12K rwxp    [ anon ]
0f996000    1172K r-xp  /usr/lib/
0fabb000      64K ---p  /usr/lib/
0facb000      12K rwxp  /usr/lib/
0face000       4K rwxp    [ anon ]
0fadf000       8K r-xp  /lib/
0fae1000      60K ---p  /lib/
0faf0000       4K r--p  /lib/
0faf1000       4K rwxp  /lib/
0fb02000      12K r-xp  /usr/lib/
0fb05000      60K ---p  /usr/lib/
0fb14000       4K rwxp  /usr/lib/
0fb25000     308K r-xp  /usr/lib/
0fb72000      60K ---p  /usr/lib/
0fb81000       8K rwxp  /usr/lib/
0fb83000       4K rwxp    [ anon ]
0fb94000     124K r-xp  /usr/lib/
0fbb3000      60K ---p  /usr/lib/
0fbc2000      12K rwxp  /usr/lib/
0fbd5000    1268K r-xp  /usr/lib/
0fd12000      60K ---p  /usr/lib/
0fd21000      36K rwxp  /usr/lib/
0fd2a000       4K rwxp    [ anon ]
0fd3b000     120K r-xp  /usr/lib/
0fd59000      64K ---p  /usr/lib/
0fd69000       4K rwxp  /usr/lib/
0fd6a000       4K rwxp    [ anon ]
0fd7b000      48K r-xp  /usr/lib/
0fd87000      60K ---p  /usr/lib/
0fd96000       4K rwxp  /usr/lib/
0fda7000    1132K r-xp  /usr/lib/
0fec2000      60K ---p  /usr/lib/
0fed1000      32K rwxp  /usr/lib/
0fed9000      16K rwxp    [ anon ]
0feed000     276K r-xp  /usr/lib/
0ff32000      64K ---p  /usr/lib/
0ff42000       8K rwxp  /usr/lib/
0ff44000       4K rwxp    [ anon ]
0ff55000     160K r-xp  /usr/lib/
0ff7d000      60K ---p  /usr/lib/
0ff8c000       8K rwxp  /usr/lib/
0ff9e000      40K r-xp  /usr/lib/
0ffa8000      64K ---p  /usr/lib/
0ffb8000       4K rwxp  /usr/lib/
0ffc9000      80K r-xp  /lib/
0ffdd000      60K ---p  /lib/
0ffec000       4K r--p  /lib/
0ffed000       4K rwxp  /lib/
0ffee000       8K rwxp    [ anon ]
10000000     244K r-xp  /bin/myapp
1004d000       8K rwxp  /bin/myapp
1004f000   14536K rwxp  [heap]
48000000     108K r-xp  /lib/
4801b000      48K rw-p    [ anon ]
4802a000       4K r--p  /lib/
4802b000       8K rwxp  /lib/
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
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, "", &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

   //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

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

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

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
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.


 Post subject: Re: Memory Leak on GTK application
PostPosted: Mon Jun 02, 2014 6:40 am 
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.

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

Joined: Mon Apr 28, 2008 5:52 am
Posts: 1048
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
- rats

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

- duma
- electric-fence
- The built-in Glib memory profiling

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 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.


 Post subject: Re: Memory Leak on GTK application
PostPosted: Tue Jun 17, 2014 8:57 am 
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.

None of us is stronger than all of us

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: Google [Bot], Yahoo [Bot] and 7 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