Re: Gtk Text Widget memory leak?




Some Guy <guy@mikepery.pr.mcs.net> writes:

> On Sun, 14 Jun 1998 valankar@bigfoot.com wrote:
> 
> > 	Hello, I'm having a slight problem with GTK's Text widget in
> > Linux. It seems when I add text to the widget, and then destroy it, the
> > memory is not freed. I'm not sure of this, but what I did to test was load
> > large files (kernel, etc) into the text widget, destroyed the widget,
> > create a new text widget, load a large file, etc... and in the process
> > doing a 'ps ux' to check memory usage. I've noticed that the %MEM field of
> > the ps output rises considerably as each file is loaded and does not go
> > down once the text widget is destroyed.
> > 
> > 	Is this normal? What should the proper way that a Text widget
> > should be destroyed? Right now I am using gtk_widget_destroy on the dialog
> > that contains the text widget (and have also tried destroying the actual
> > text widget). Any help appreciated.
> 
> I've never tried this but I think you have to delete the text first, then
> destroy the widget.  This is done with something like
> 	gtk_text_set_point(GTK_TEXT(txt), 0);
> 	gtk_text_forward_delete(GTK_TEXT(txt), -1); // -1, to end of text.
> or something of that nature.

Hmmm. That should not be necessary. (And actually, won't work because
of the way text manages things internally)

The problem seems to be that the code to free up the text widget's
internal structures, if it ever existed, somehow was removed.

The following patch fixes that up, at least with my tests with
testgtk. (The memory even gets returned to the system on my
machine, apropos to discussion on gnome-list...)

Regards,
                                        Owen

Index: gtktext.c
===================================================================
RCS file: /debian/home/gnomecvs/gtk+/gtk/gtktext.c,v
retrieving revision 1.45
diff -u -r1.45 gtktext.c
--- gtktext.c	1998/06/11 17:52:12	1.45
+++ gtktext.c	1998/06/15 19:21:08
@@ -159,6 +159,7 @@
 static void  gtk_text_class_init     (GtkTextClass   *klass);
 static void  gtk_text_init           (GtkText        *text);
 static void  gtk_text_destroy        (GtkObject      *object);
+static void  gtk_text_finalize       (GtkObject      *object);
 static void  gtk_text_realize        (GtkWidget      *widget);
 static void  gtk_text_unrealize      (GtkWidget      *widget);
 static void  gtk_text_style_set	     (GtkWidget      *widget,
@@ -274,6 +275,7 @@
 static void process_exposes (GtkText *text);
 
 /* Cache Management. */
+static void   free_cache        (GtkText* text);
 static GList* remove_cache_line (GtkText* text, GList* list);
 
 /* Key Motion. */
@@ -460,6 +462,7 @@
   parent_class = gtk_type_class (gtk_editable_get_type ());
 
   object_class->destroy = gtk_text_destroy;
+  object_class->finalize = gtk_text_finalize;
 
   widget_class->realize = gtk_text_realize;
   widget_class->unrealize = gtk_text_unrealize;
@@ -934,6 +937,38 @@
 }
 
 static void
+gtk_text_finalize (GtkObject *object)
+{
+  GtkText *text;
+  GList *tmp_list;
+
+  g_return_if_fail (object != NULL);
+  g_return_if_fail (GTK_IS_TEXT (object));
+
+  text = (GtkText *)object;
+
+  /* Clean up the internal structures */
+  g_free (text->text);
+  free_cache (text);
+
+  tmp_list = text->text_properties;
+  while (tmp_list)
+    {
+      g_mem_chunk_free (text_property_chunk, tmp_list->data);
+      tmp_list = tmp_list->next;
+    }
+  
+  g_list_free (text->text_properties);
+
+  if (text->scratch_buffer)
+    g_free (text->scratch_buffer);
+
+  g_list_free (text->tab_stops);
+  
+  GTK_OBJECT_CLASS(parent_class)->finalize (object);
+}
+
+static void
 gtk_text_realize (GtkWidget *widget)
 {
   GtkText *text;
@@ -1112,6 +1147,9 @@
 
   gdk_gc_destroy (text->gc);
   text->gc = NULL;
+
+  gdk_pixmap_unref (text->line_wrap_bitmap);
+  gdk_pixmap_unref (text->line_arrow_bitmap);
 
   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
     (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);






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