Fix various memory leaks, mostly in plugins
authorAndy Spencer <andy753421@gmail.com>
Sat, 6 Feb 2010 11:46:34 +0000 (11:46 +0000)
committerAndy Spencer <andy753421@gmail.com>
Sat, 6 Feb 2010 11:46:34 +0000 (11:46 +0000)
- Keep references to objects in plugins and remove/free them when the
  plugins are destroyed.
- g_close_dir in gis-plugin.c

src/gis-plugin.c
src/objects/gis-marker.c
src/objects/gis-tile.c
src/plugins/env.c
src/plugins/env.h
src/plugins/test.c
src/plugins/test.h

index a2e2cc3..255c0be 100644 (file)
@@ -127,6 +127,7 @@ GList *gis_plugins_available(GisPlugins *self)
                                g_strfreev(parts);
                        }
                }
+               g_dir_close(dir);
        }
        return list;
 }
index 0033080..42639ad 100644 (file)
 #include "gis-marker.h"
 
 /* GisMarker */
-G_DEFINE_TYPE(GisMarker, gis_marker, GIS_TYPE_OBJECT);
-static void gis_marker_init(GisMarker *self) { }
-
-static void gis_marker_finalize(GObject *_self);
-static void gis_marker_class_init(GisMarkerClass *klass)
-{
-       G_OBJECT_CLASS(klass)->finalize = gis_marker_finalize;
-}
-
 GisMarker *gis_marker_new(const gchar *label)
 {
+       //g_debug("GisMarker: new - %s", label);
        static const int RADIUS =   4;
        static const int WIDTH  = 100;
        static const int HEIGHT =  20;
@@ -49,11 +41,20 @@ GisMarker *gis_marker_new(const gchar *label)
        return self;
 }
 
+G_DEFINE_TYPE(GisMarker, gis_marker, GIS_TYPE_OBJECT);
+static void gis_marker_init(GisMarker *self) { }
+
 static void gis_marker_finalize(GObject *_self)
 {
        GisMarker *self = GIS_MARKER(_self);
-       cairo_surface_destroy(cairo_get_target(self->cairo));
+       //g_debug("GisMarker: finalize - %s", self->label);
+       cairo_surface_t *surface = cairo_get_target(self->cairo);
+       cairo_surface_destroy(surface);
        cairo_destroy(self->cairo);
        g_free(self->label);
-       g_free(self);
+}
+
+static void gis_marker_class_init(GisMarkerClass *klass)
+{
+       G_OBJECT_CLASS(klass)->finalize = gis_marker_finalize;
 }
index d11af4f..b2f96d3 100644 (file)
@@ -179,6 +179,7 @@ GisTile *gis_tile_gc(GisTile *self, time_t atime,
        return self;
 }
 
+/* Use GObject for this */
 void gis_tile_free(GisTile *self, GisTileFreeFunc free_func, gpointer user_data)
 {
        if (!self)
index 92588ce..a15c023 100644 (file)
@@ -79,17 +79,20 @@ GisPluginEnv *gis_plugin_env_new(GisViewer *viewer, GisPrefs *prefs)
 {
        g_debug("GisPluginEnv: new");
        GisPluginEnv *self = g_object_new(GIS_TYPE_PLUGIN_ENV, NULL);
-       self->viewer = viewer;
+       self->viewer = g_object_ref(viewer);
 
-       /* Load blank background texture */
+       /* Create objects */
+       GisCallback *callback   = gis_callback_new(expose, self);
+       GisTile     *background = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
        glGenTextures(1, &self->tex);
-       self->background = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
-       self->background->data = &self->tex;
+       background->data = &self->tex;
 
        /* Add renderers */
-       GisCallback *callback = gis_callback_new(expose, self);
-       gis_viewer_add(viewer, GIS_OBJECT(callback), GIS_LEVEL_BACKGROUND, 0);
-       gis_viewer_add(viewer, GIS_OBJECT(self->background), GIS_LEVEL_BACKGROUND, 0);
+       gpointer ref1, ref2;
+       ref1 = gis_viewer_add(viewer, GIS_OBJECT(callback),   GIS_LEVEL_BACKGROUND, FALSE);
+       ref2 = gis_viewer_add(viewer, GIS_OBJECT(background), GIS_LEVEL_BACKGROUND, FALSE);
+       self->refs = g_list_prepend(self->refs, ref1);
+       self->refs = g_list_prepend(self->refs, ref2);
 
        return self;
 }
@@ -119,11 +122,19 @@ static void gis_plugin_env_dispose(GObject *gobject)
        g_debug("GisPluginEnv: dispose");
        GisPluginEnv *self = GIS_PLUGIN_ENV(gobject);
        /* Drop references */
+       if (self->viewer) {
+               for (GList *cur = self->refs; cur; cur = cur->next)
+                       gis_viewer_remove(self->viewer, cur->data);
+               g_list_free(self->refs);
+               g_object_unref(self->viewer);
+               glDeleteTextures(1, &self->tex);
+               self->viewer = NULL;
+       }
        G_OBJECT_CLASS(gis_plugin_env_parent_class)->dispose(gobject);
 }
 static void gis_plugin_env_class_init(GisPluginEnvClass *klass)
 {
        g_debug("GisPluginEnv: class_init");
        GObjectClass *gobject_class = (GObjectClass*)klass;
-       gobject_class->dispose  = gis_plugin_env_dispose;
+       gobject_class->dispose = gis_plugin_env_dispose;
 }
index 9ac31e6..7b73a2e 100644 (file)
@@ -37,6 +37,7 @@ struct _GisPluginEnv {
        GisViewer *viewer;
        guint      tex;
        GisTile   *background;
+       GList     *refs;
 };
 
 struct _GisPluginEnvClass {
index 87b0a61..2cbc140 100644 (file)
@@ -30,12 +30,12 @@ GisPluginTest *gis_plugin_test_new(GisViewer *viewer)
 {
        g_debug("GisPluginTest: new");
        GisPluginTest *self = g_object_new(GIS_TYPE_PLUGIN_TEST, NULL);
-       self->viewer = viewer;
+       self->viewer = g_object_ref(viewer);
 
        GisMarker *marker = gis_marker_new("St. Charles");
        gis_point_set_lle(gis_object_center(GIS_OBJECT(marker)), 38.841847, -90.491982, 0);
        GIS_OBJECT(marker)->lod = EARTH_R/4;
-       gis_viewer_add(self->viewer, GIS_OBJECT(marker), GIS_LEVEL_OVERLAY, 0);
+       self->marker = gis_viewer_add(self->viewer, GIS_OBJECT(marker), GIS_LEVEL_OVERLAY, 0);
 
        return self;
 }
@@ -59,7 +59,20 @@ static void gis_plugin_test_init(GisPluginTest *self)
 {
        g_debug("GisPluginTest: init");
 }
+static void gis_plugin_test_dispose(GObject *_self)
+{
+       g_debug("GisPluginTest: dispose");
+       GisPluginTest *self = GIS_PLUGIN_TEST(_self);
+       if (self->viewer) {
+               gis_viewer_remove(self->viewer, self->marker);
+               g_object_unref(self->viewer);
+               self->viewer = NULL;
+       }
+       G_OBJECT_CLASS(gis_plugin_test_parent_class)->finalize(_self);
+}
 static void gis_plugin_test_class_init(GisPluginTestClass *klass)
 {
        g_debug("GisPluginTest: class_init");
+       GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
+       gobject_class->dispose = gis_plugin_test_dispose;
 }
index 4c5579c..623454a 100644 (file)
@@ -35,6 +35,7 @@ struct _GisPluginTest {
 
        /* instance members */
        GisViewer *viewer;
+       gpointer   marker;
 };
 
 struct _GisPluginTestClass {