[f-spot/rubenv-gsoc-2009: 46/86] Plug some memory leaks, hook up code to valgrind.



commit cabedd73685550231095b46cd40b788c0990fefa
Author: Ruben Vermeersch <ruben savanne be>
Date:   Thu Jul 30 17:25:38 2009 +0200

    Plug some memory leaks, hook up code to valgrind.

 lib/libfspotraw/.gitignore             |    2 +
 lib/libfspotraw/Makefile.am            |   43 ++++++++++++++++++++++++++++++++
 lib/libfspotraw/fspot-librawloader.cpp |   17 +++++++++++-
 lib/libfspotraw/memory-test.c          |   27 ++++++++++++++++++++
 src/Loaders/LibrawImageLoader.cs       |    7 +++++
 5 files changed, 94 insertions(+), 2 deletions(-)
---
diff --git a/lib/libfspotraw/.gitignore b/lib/libfspotraw/.gitignore
index 937361d..ec55d90 100644
--- a/lib/libfspotraw/.gitignore
+++ b/lib/libfspotraw/.gitignore
@@ -4,7 +4,9 @@
 /.libs
 /*.o
 /*.lo
+/*.vo
 /*.la
 /LibRaw-*.tar.gz
 /fspot-librawloader-marshal.c
 /fspot-librawloader-marshal.h
+/memory-test
diff --git a/lib/libfspotraw/Makefile.am b/lib/libfspotraw/Makefile.am
index 2ba791d..311f83e 100644
--- a/lib/libfspotraw/Makefile.am
+++ b/lib/libfspotraw/Makefile.am
@@ -57,3 +57,46 @@ MARSHAL_GENERATED = fspot-librawloader-marshal.c fspot-librawloader-marshal.h
 BUILT_SOURCES = $(MARSHAL_GENERATED)
 
 CLEANFILES = $(MARSHAL_GENERATED)
+
+# The stuff below builds a special, non-optimized version of libfspotraw,
+# for valgrinding purposes.
+
+libraw_dcraw_common.vo: LibRaw/internal/dcraw_common.cpp
+	gcc -g -O0 -c -o $@ $< -I LibRaw
+
+libraw_foveon.vo: LibRaw/internal/foveon.cpp
+	gcc -g -O0 -c -o $@ $< -I LibRaw
+
+libraw_cxx.vo: LibRaw/src/libraw_cxx.cpp
+	g++ -g -O0 -c -o $@ $< -I LibRaw
+
+libraw_c_api.vo: LibRaw/src/libraw_c_api.cpp
+	g++ -g -O0 -c -o $@ $< -I LibRaw
+
+libraw_fileio.vo: LibRaw/internal/dcraw_fileio.cpp
+	g++ -g -O0 -c -o $@ $< -I LibRaw
+
+fspot-librawloader_marshal.vo: fspot-librawloader-marshal.c
+	gcc -g -O0 -c -o $@ $< `pkg-config --cflags gtk+-2.0`
+
+fspot-librawloader.vo: fspot-librawloader.cpp fspot-librawloader.h
+	g++ -g -O0 -c -o $@ $< `pkg-config --cflags gtk+-2.0` -I LibRaw
+
+memory-test.vo: memory-test.c
+	gcc -g -O0 -c -o $@ $< `pkg-config --cflags gtk+-2.0`
+
+MEMORY_TEST_OBJECTS =				\
+	libraw_dcraw_common.vo			\
+	libraw_foveon.vo				\
+	libraw_cxx.vo					\
+	libraw_c_api.vo					\
+	libraw_fileio.vo				\
+	fspot-librawloader_marshal.vo	\
+	fspot-librawloader.vo			\
+	memory-test.vo
+
+memory-test: $(MEMORY_TEST_OBJECTS)
+	g++ -g -O0 -o memory-test $(MEMORY_TEST_OBJECTS) `pkg-config --cflags --libs gtk+-2.0` -ljpeg -lm -llcms -lstdc++
+
+valgrind: memory-test
+	G_SLICE=always-malloc G_DEBUG=gc-friendly valgrind --tool=memcheck --leak-check=full --num-callers=20 $O ./memory-test $F
diff --git a/lib/libfspotraw/fspot-librawloader.cpp b/lib/libfspotraw/fspot-librawloader.cpp
index c0da10e..e6e5907 100644
--- a/lib/libfspotraw/fspot-librawloader.cpp
+++ b/lib/libfspotraw/fspot-librawloader.cpp
@@ -41,6 +41,7 @@ static void fspot_librawloader_dispose (GObject *object);
 static void fspot_librawloader_finalize (GObject *object);
 
 static void open_if_needed (FSpotLibrawLoader *self);
+static void pixbuf_freed (guchar *pixels, gpointer data);
 
 static int libraw_progress_callback (void *user_data, enum LibRaw_progress p, int iteration, int expected);
 
@@ -163,6 +164,7 @@ fspot_librawloader_dispose (GObject *object)
 {
 	FSpotLibrawLoader *self = FSPOT_LIBRAWLOADER (object);
 
+	self->priv->aborted = true;
 	self->priv->raw_proc->recycle ();
 
 	G_OBJECT_CLASS (fspot_librawloader_parent_class)->dispose (object);
@@ -173,6 +175,7 @@ fspot_librawloader_finalize (GObject *object)
 {
 	FSpotLibrawLoader *self = FSPOT_LIBRAWLOADER (object);
 
+	self->priv->raw_proc->recycle ();
 	delete self->priv->raw_proc;
 	g_free (self->priv->filename);
 
@@ -205,6 +208,9 @@ fspot_librawloader_load_embedded (FSpotLibrawLoader *self, int *orientation)
 	pixbuf = gdk_pixbuf_copy (gdk_pixbuf_loader_get_pixbuf (loader));
 	*orientation = self->priv->raw_proc->imgdata.sizes.flip;
 
+	g_object_unref (loader);
+	g_free (image);
+
 	return pixbuf;
 }
 
@@ -232,12 +238,19 @@ fspot_librawloader_load_full (FSpotLibrawLoader *self)
 									   image->width,
 									   image->height,
 									   image->width * 3, /* rowstride */
-									   (GdkPixbufDestroyNotify) g_free,
-									   NULL);
+									   (GdkPixbufDestroyNotify) pixbuf_freed,
+									   image);
 
 	return pixbuf;
 }
 
+static void
+pixbuf_freed (guchar *pixels, gpointer data)
+{
+	libraw_processed_image_t *image = (libraw_processed_image_t *)data;
+	g_free (image);
+}
+
 FSpotLibrawLoader *
 fspot_librawloader_new (const gchar *filename)
 {
diff --git a/lib/libfspotraw/memory-test.c b/lib/libfspotraw/memory-test.c
new file mode 100644
index 0000000..4c1aaa6
--- /dev/null
+++ b/lib/libfspotraw/memory-test.c
@@ -0,0 +1,27 @@
+#include <glib-object.h>
+#include "fspot-librawloader.h"
+
+int main (int argc, char **argv) {
+	FSpotLibrawLoader *loader;
+	GdkPixbuf *pixbuf;
+
+	g_type_init ();
+
+	g_assert (argc == 2);
+	loader = g_object_new (FSPOT_TYPE_LIBRAWLOADER, "filename", argv[1], NULL);
+
+	int orientation;
+	pixbuf = fspot_librawloader_load_embedded (loader, &orientation);
+	g_object_unref (pixbuf);
+	pixbuf = NULL;
+
+	pixbuf = fspot_librawloader_load_full (loader);
+	g_object_unref (pixbuf);
+	pixbuf = NULL;
+
+	g_object_unref (loader);
+	loader = NULL;
+
+
+	return 0;
+}
diff --git a/src/Loaders/LibrawImageLoader.cs b/src/Loaders/LibrawImageLoader.cs
index 23cbdc9..e6c0b05 100644
--- a/src/Loaders/LibrawImageLoader.cs
+++ b/src/Loaders/LibrawImageLoader.cs
@@ -68,6 +68,12 @@ namespace FSpot.Loaders {
 			loader = new NativeLibrawLoader (uri.AbsolutePath);
 		}
 
+		~LibrawImageLoader ()
+		{
+			if (!is_disposed)
+				Dispose ();
+		}
+
 		public ImageLoaderItem Load (ImageLoaderItem items, bool async)
 		{
 			if (is_disposed)
@@ -88,6 +94,7 @@ namespace FSpot.Loaders {
 			is_disposed = true;
 			if (loader != null) {
 				loader.Aborted = true;
+				loader.Dispose ();
 				loader = null;
 			}
 			if (thumbnail != null)



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