Re: checking for memory leaks in gtk


I dont have any pixmaps or images in my sample application. I have
attached my program. Its a simple program having two windows. First
window has 2 buttons open and close. When open is clicked it opens
another widow which has a close button. When close button on second
window is pressed second window is destroyed.

Its here that i observe leaks. Memory increases when second window is
created and when its destroyed memory is not freed at all on DirectFB

Am i calling the correct APIs? Help is greatly appreciated. Somehow i
feel destroy is not happening properly!

Harinandan S

On Wed, May 14, 2008 at 12:49 PM,  <jcupitt gmail com> wrote:
> 2008/5/14 Harinandan S <harinandans gmail com>:
> >  I observed continuous accumulation of memory while running GTK over
> >  DirectFB on MontaVista linux on TI Davinci. I also observed
> >  significant memory accumulation while running GTK-win32 on Windows XP.
> >  But to my surprise there is no accumulation of memory
> >  on GTK-X window running on RHEL 4 on x86.
> I'm only speculating, but some resources (server-side images or
> pixmaps, for example) are kept by the X server and won't show
> significant memuse for your application in "top". With other backends,
> like win32 or directfb, you may well see these objects in your own
> memory space.
> I'd suggest running with valgrind and looking for leaks with that. For
> what it's worth, my largish project (250 kloc or so) does not seem to
> leak significantly under linux or windows.
> John
#include <gtk/gtk.h>

create_window1 (void);

create_window2 (void);

on_open_clicked                        (GtkButton       *button,
                                        gpointer         user_data)
	/* open a pop-up window */
	GtkWidget *window2 = create_window2 ();
    gtk_widget_show (window2);


on_close_clicked                       (GtkButton       *button,
                                        gpointer         user_data)
	GtkWidget *window = user_data;

create_window1 (void)
  GtkWidget *window1;
  GtkWidget *fixed1;
  GtkWidget *open;
  GtkWidget *close;

  window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_widget_set_size_request (window1, 640, 480);
  gtk_window_set_title (GTK_WINDOW (window1), "window1");

  fixed1 = gtk_fixed_new ();
  gtk_widget_show (fixed1);
  gtk_container_add (GTK_CONTAINER (window1), fixed1);

  open = gtk_button_new_with_mnemonic ("open");
  gtk_widget_show (open);
  gtk_fixed_put (GTK_FIXED (fixed1), open, 208, 200);
  gtk_widget_set_size_request (open, 64, 40);

  close = gtk_button_new_with_mnemonic ("close");
  gtk_widget_show (close);
  gtk_fixed_put (GTK_FIXED (fixed1), close, 296, 200);
  gtk_widget_set_size_request (close, 72, 40);

  /* send pointer to window1 to callback function */

  g_signal_connect ((gpointer) open, "clicked",
                    G_CALLBACK (on_open_clicked),
  g_signal_connect ((gpointer) close, "clicked",
                    G_CALLBACK (on_close_clicked),

  return window1;

create_window2 (void)
  GtkWidget *window2;
  GtkWidget *fixed2;
  GtkWidget *close;

  window2 = gtk_window_new (GTK_WINDOW_POPUP);
  gtk_widget_set_size_request (window2, 320, 240);
  gtk_window_set_title (GTK_WINDOW (window2), "window2");

  fixed2 = gtk_fixed_new ();
  gtk_widget_show (fixed2);
  gtk_container_add (GTK_CONTAINER (window2), fixed2);

  close = gtk_button_new_with_mnemonic ("close");
  gtk_widget_show (close);
  gtk_fixed_put (GTK_FIXED (fixed2), close, 144, 112);
  gtk_widget_set_size_request (close, 80, 48);

  g_signal_connect ((gpointer) close, "clicked",
                    G_CALLBACK (on_close_clicked),

  return window2;

main (int argc, char *argv[])
  GtkWidget *window1;

  gtk_init (&argc, &argv);

  /* create top window*/
  window1 = create_window1 ();
  gtk_widget_show (window1);
  g_signal_connect ((gpointer) window1, "destroy", G_CALLBACK(gtk_main_quit),

  gtk_main ();

  return 0;

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