mem leak review (blast from the past)

Hi all,

        The following code snippet is from a post 
to `gtk-devel-list' by Sven Neumann in 
september 2001 conserning a memory leak.

        What I've noticed is that if the
application in question does not have
`focus' ; thats when it starts leaking.

        If there was a fix to that bug
since version 1.3.8 I would really like
to know in which version and if possible;
which files. (right now I'm struggling
to make sure that app #1 doesn't 
`gtk_label_set_text' when app #2 has focus etc
etc ... real headache).

Thanks in advance,

Of course; if I figure it out first
I'll send in a patch. 

           THIS LEAKS...
#include <stdio.h>
#include <gtk/gtk.h>

GtkWidget *label;
guint count, timeout_id = 0;

gboolean set_text (gpointer foo)
  char buf[32];
  sprintf(buf, "%03d", ++count);
  gtk_label_set_text (GTK_LABEL(label), buf);

  return TRUE;

void click_callback (GtkWidget *widget,
                     gpointer   data)
    if (timeout_id)
        timeout_id = 0;
        timeout_id = g_timeout_add (10, set_text, 0);

void destroy (GtkWidget *widget,
              gpointer   data)

int main (int   argc,
          char *argv[])
    GtkWidget *window;
    GtkWidget *button;
    GtkWidget *vbox;
    gtk_init(&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

    label = gtk_label_new ("foo");
    count = 0;

    vbox = gtk_hbox_new (FALSE, 0);
    gtk_signal_connect (GTK_OBJECT (window), "destroy",
                        GTK_SIGNAL_FUNC (destroy), NULL);
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);
    button = gtk_button_new_with_label ("Leak!");
    gtk_signal_connect (GTK_OBJECT (button), "clicked",
                        GTK_SIGNAL_FUNC (click_callback), NULL);
    gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 5);
    gtk_box_pack_end (GTK_BOX (vbox), button, TRUE, TRUE, 5);
    gtk_container_add (GTK_CONTAINER (window), vbox);
    gtk_widget_show (label);
    gtk_widget_show (button);
    gtk_widget_show (vbox);
    gtk_widget_show (window);
    gtk_main ();

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