Add grits_object_destroy functions and fix memory leaks
authorAndy Spencer <andy753421@gmail.com>
Mon, 11 Feb 2013 06:12:15 +0000 (06:12 +0000)
committerAndy Spencer <andy753421@gmail.com>
Mon, 11 Feb 2013 06:24:56 +0000 (06:24 +0000)
src/objects/grits-object.c
src/objects/grits-object.h
src/plugins/elev.c
src/plugins/env.c
src/plugins/map.c
src/plugins/sat.c
src/plugins/test.c

index 2812b9e..55f3975 100644 (file)
@@ -172,6 +172,13 @@ void grits_object_set_cursor(GritsObject *object, GdkCursorType cursor)
        object->cursor = gdk_cursor_new(cursor);
 }
 
+void grits_object_destroy(GritsObject *object)
+{
+       if (object->viewer)
+               grits_viewer_remove(object->viewer, object);
+       g_object_unref(object);
+}
+
 /* Event handling */
 void grits_object_pick(GritsObject *object, GritsOpenGL *opengl)
 {
index 728ac72..2c11cd3 100644 (file)
@@ -102,6 +102,30 @@ void grits_object_queue_draw(GritsObject *object);
 void grits_object_set_cursor(GritsObject *object, GdkCursorType cursor);
 
 /**
+ * grits_object_destroy:
+ * @object: The #GritsObject to destroy
+ *
+ * Removes the widget from it's current viewer (if it has one) and decrements
+ * it's reference count.
+ */
+void grits_object_destroy(GritsObject *object);
+
+/**
+ * grits_object_destroy_pointer:
+ * @object: The pointer to the #GritsObject to destroy
+ *
+ * This functions the same as grits_object_destroy, except that the location of
+ * the object is set to null before proceeding.
+ */
+#define grits_object_destroy_pointer(object) ({           \
+       if (*object) {                                    \
+               GritsObject *tmp = GRITS_OBJECT(*object); \
+               *object = NULL;                           \
+               grits_object_destroy(tmp);                \
+       }                                                 \
+})
+
+/**
  * grits_object_center:
  * @object: The #GritsObject to get the center of
  * 
index 3634df6..f360451 100644 (file)
@@ -272,8 +272,7 @@ static void grits_plugin_elev_dispose(GObject *gobject)
                if (LOAD_BIL)
                        grits_viewer_clear_height_func(viewer);
                if (LOAD_TEX)
-                       grits_viewer_remove(viewer, GRITS_OBJECT(elev->tiles));
-               g_object_unref(elev->tiles);
+                       grits_object_destroy_pointer(&elev->tiles);
                g_object_unref(viewer);
        }
        G_OBJECT_CLASS(grits_plugin_elev_parent_class)->dispose(gobject);
index 658881d..717e9d7 100644 (file)
@@ -375,8 +375,7 @@ static void grits_plugin_env_dispose(GObject *gobject)
        /* Drop references */
        if (env->viewer) {
                for (GList *cur = env->refs; cur; cur = cur->next)
-                       grits_viewer_remove(env->viewer, cur->data);
-               g_list_free_full(env->refs, g_object_unref);
+                       grits_object_destroy_pointer(&cur->data);
                g_object_unref(env->viewer);
                g_object_unref(env->prefs);
                glDeleteTextures(1, &env->tex);
index 23e8b24..b9a5495 100644 (file)
@@ -194,8 +194,7 @@ static void grits_plugin_map_dispose(GObject *gobject)
                //grits_http_abort(map->wms->http);
                g_thread_pool_free(map->threads, TRUE, TRUE);
                map->viewer = NULL;
-               grits_viewer_remove(viewer, GRITS_OBJECT(map->tiles));
-               g_object_unref(map->tiles);
+               grits_object_destroy_pointer(&map->tiles);
                g_object_unref(viewer);
        }
        G_OBJECT_CLASS(grits_plugin_map_parent_class)->dispose(gobject);
index bf3b598..a57277d 100644 (file)
@@ -173,8 +173,7 @@ static void grits_plugin_sat_dispose(GObject *gobject)
                grits_http_abort(sat->wms->http);
                g_thread_pool_free(sat->threads, TRUE, TRUE);
                sat->viewer = NULL;
-               grits_viewer_remove(viewer, GRITS_OBJECT(sat->tiles));
-               g_object_unref(sat->tiles);
+               grits_object_destroy_pointer(&sat->tiles);
                g_object_unref(viewer);
        }
        G_OBJECT_CLASS(grits_plugin_sat_parent_class)->dispose(gobject);
index 2c3e52f..8d12dc1 100644 (file)
@@ -177,12 +177,9 @@ static void grits_plugin_test_dispose(GObject *_test)
        if (test->viewer) {
                GritsViewer *viewer = test->viewer;
                test->viewer = NULL;
-               grits_viewer_remove(viewer, GRITS_OBJECT(test->marker));
-               grits_viewer_remove(viewer, GRITS_OBJECT(test->poly));
-               grits_viewer_remove(viewer, GRITS_OBJECT(test->line));
-               g_object_unref(test->marker);
-               g_object_unref(test->poly);
-               g_object_unref(test->line);
+               grits_object_destroy_pointer(&test->marker);
+               grits_object_destroy_pointer(&test->poly);
+               grits_object_destroy_pointer(&test->line);
                g_object_unref(viewer);
        }
        G_OBJECT_CLASS(grits_plugin_test_parent_class)->dispose(_test);