checking pixbuf loaders for memleaks



Here is a patch which adds some simple leak checking
to pixbuf-read.c and pixbuf-randomly-modified.c. I'm 
posting it here rather than committing it, since I'm
not sure how portable mallinfo() is, which I've used.
Any information on this ? I guess we could always 
#ifdef HAVE_MALLINFO it if necessary. In case you
wonder: I'm calling each loader twice in order to
avoid false positives from one-time allocations done
by e.g. gtype. 

The patch has already helped me find and plug two 
memleaks in the xbm loader.

Matthias 


Index: pixbuf-read.c
===================================================================
RCS file: /cvs/gnome/gtk+/tests/pixbuf-read.c,v
retrieving revision 1.1
diff -u -b -B -p -r1.1 pixbuf-read.c
--- pixbuf-read.c	5 May 2002 00:58:48 -0000	1.1
+++ pixbuf-read.c	31 May 2002 21:57:11 -0000
@@ -22,20 +22,30 @@
 #include "gdk-pixbuf/gdk-pixbuf.h"
 #include <stdio.h>
 #include <stdlib.h>
+#include <malloc.h>
 
 static gboolean
 test_loader (const guchar *bytes, gsize len, GError **err)
 {
   GdkPixbufLoader *loader;
+  int delta, current_allocation, pass;
 
+  for (pass = 0; pass < 2; pass++) {
+    
+    current_allocation = mallinfo().uordblks;
   loader = gdk_pixbuf_loader_new ();
   gdk_pixbuf_loader_write (loader, bytes, len, err);
   if (*err)
       return FALSE;
   
   gdk_pixbuf_loader_close (loader, err);
+    g_object_unref (G_OBJECT (loader));
   if (*err)
     return FALSE;
+    delta = mallinfo().uordblks - current_allocation;
+  }
+  if (delta > 0)
+    g_print ("%d bytes leaked\n", delta);
 
   return TRUE;
 }
Index: pixbuf-randomly-modified.c
===================================================================
RCS file: /cvs/gnome/gtk+/tests/pixbuf-randomly-modified.c,v
retrieving revision 1.1
diff -u -b -B -p -r1.1 pixbuf-randomly-modified.c
--- pixbuf-randomly-modified.c	5 May 2002 00:58:48 -0000	1.1
+++ pixbuf-randomly-modified.c	31 May 2002 21:57:11 -0000
@@ -25,6 +25,7 @@
 #include <stdlib.h>
 #include <time.h>
 #include <string.h>
+#include <malloc.h>
 
 static void
 disaster (const char *what)
@@ -45,6 +46,7 @@ randomly_modify (const guchar *image, gu
     {
       FILE *f;
       GdkPixbufLoader *loader;
+      int current_allocation, delta, pass;
       
       guint index = g_random_int_range (0, size);
       guchar byte = g_random_int_range (0, 256);
@@ -58,10 +60,16 @@ randomly_modify (const guchar *image, gu
 	disaster ("fwrite");
       fclose (f);
 
+      for (pass = 0; pass < 2; pass++) {
+	current_allocation = mallinfo().uordblks;
       loader = gdk_pixbuf_loader_new ();
       gdk_pixbuf_loader_write (loader, img_copy, size, NULL);
       gdk_pixbuf_loader_close (loader, NULL);
       g_object_unref (G_OBJECT (loader));
+	delta = mallinfo().uordblks - current_allocation;
+      }
+      if (delta > 0)
+	g_print ("%d bytes leaked\n", delta);
     }
   g_free (img_copy);
 }


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