From 72c9f88019cd12c36e08483af0de0b6b9300e2d1 Mon Sep 17 00:00:00 2001
From: Neil Roberts <neil@openedhand.com>
Date: Mon, 22 Sep 2008 16:36:27 +0000
Subject: [PATCH] 	Bug 856 - Teardown sequence is borked

	* clutter/clutter-main.c: Don't free the ClutterMainContext so
	that the main loop can be restarted.

	* clutter/eglx/clutter-backend-egl.c:
	* clutter/eglnative/clutter-backend-egl.c: Register an atexit
	handler which disposes the backend object so that we are still
	guaranteed to call eglTerminate on GLES.
---
 ChangeLog                               | 12 ++++++++++
 clutter/clutter-main.c                  | 32 +++----------------------
 clutter/eglnative/clutter-backend-egl.c |  9 +++++++
 clutter/eglx/clutter-backend-egl.c      |  9 +++++++
 4 files changed, 33 insertions(+), 29 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 0b89dd4c8..efa1a162c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2008-09-22  Neil Roberts  <neil@linux.intel.com>
+
+	Bug 856 - Teardown sequence is borked
+
+	* clutter/clutter-main.c: Don't free the ClutterMainContext so
+	that the main loop can be restarted.
+
+	* clutter/eglx/clutter-backend-egl.c: 
+	* clutter/eglnative/clutter-backend-egl.c: Register an atexit
+	handler which disposes the backend object so that we are still
+	guaranteed to call eglTerminate on GLES.
+
 2008-09-22  Neil Roberts  <neil@linux.intel.com>
 
 	* tests/test-unproject.c (on_event): 
diff --git a/clutter/clutter-main.c b/clutter/clutter-main.c
index b2ad2e016..ea0af7405 100644
--- a/clutter/clutter-main.c
+++ b/clutter/clutter-main.c
@@ -395,29 +395,6 @@ _clutter_do_pick (ClutterStage   *stage,
   return clutter_get_actor_by_gid (id);
 }
 
-static void
-clutter_context_free (ClutterMainContext *context)
-{
-  /* this will take care of destroying the stage */
-  g_object_unref (context->backend);
-  context->backend = NULL;
-
-  clutter_id_pool_free (context->id_pool);
-  context->id_pool = NULL;
-
-  g_object_unref (context->font_map);
-  context->font_map = NULL;
-
-#ifdef CLUTTER_ENABLE_DEBUG
-  g_timer_destroy (context->timer);
-#endif
-  
-  /* XXX: The cleaning up of the event queue should be moved here from
-          the backend base class. */
-
-  g_free (context);
-}
-
 PangoContext *
 _clutter_context_create_pango_context (ClutterMainContext *self)
 {
@@ -473,9 +450,11 @@ clutter_main_level (void)
 void
 clutter_main (void)
 {
-  ClutterMainContext *context = CLUTTER_CONTEXT ();
   GMainLoop *loop;
 
+  /* Make sure there is a context */
+  CLUTTER_CONTEXT ();
+
   if (!clutter_is_initialized)
     {
       g_warning ("Called clutter_main() but Clutter wasn't initialised.  "
@@ -510,11 +489,6 @@ clutter_main (void)
 
   clutter_main_loop_level--;
 
-  if (clutter_main_loop_level == 0)
-    {
-      clutter_context_free (context);
-    }
-
   CLUTTER_MARK ();
 }
 
diff --git a/clutter/eglnative/clutter-backend-egl.c b/clutter/eglnative/clutter-backend-egl.c
index 9f8508fea..844445305 100644
--- a/clutter/eglnative/clutter-backend-egl.c
+++ b/clutter/eglnative/clutter-backend-egl.c
@@ -13,6 +13,13 @@ static ClutterBackendEGL *backend_singleton = NULL;
 
 G_DEFINE_TYPE (ClutterBackendEGL, clutter_backend_egl, CLUTTER_TYPE_BACKEND);
 
+static void
+clutter_backend_at_exit (void)
+{
+  if (backend_singleton)
+    g_object_run_dispose (G_OBJECT (backend_singleton));
+}
+
 static gboolean
 clutter_backend_egl_pre_parse (ClutterBackend  *backend,
                                GError         **error)
@@ -33,6 +40,8 @@ clutter_backend_egl_post_parse (ClutterBackend  *backend,
 			  &backend_egl->egl_version_major, 
 			  &backend_egl->egl_version_minor);
   
+  g_atexit (clutter_backend_at_exit);
+
   if (status != EGL_TRUE)
     {
       g_set_error (error, CLUTTER_INIT_ERROR,
diff --git a/clutter/eglx/clutter-backend-egl.c b/clutter/eglx/clutter-backend-egl.c
index cf44b2456..8aa93348a 100644
--- a/clutter/eglx/clutter-backend-egl.c
+++ b/clutter/eglx/clutter-backend-egl.c
@@ -13,6 +13,13 @@ static ClutterBackendEGL *backend_singleton = NULL;
 
 G_DEFINE_TYPE (ClutterBackendEGL, clutter_backend_egl, CLUTTER_TYPE_BACKEND_X11);
 
+static void
+clutter_backend_at_exit (void)
+{
+  if (backend_singleton)
+    g_object_run_dispose (G_OBJECT (backend_singleton));
+}
+
 static gboolean
 clutter_backend_egl_post_parse (ClutterBackend  *backend,
                                 GError         **error)
@@ -31,6 +38,8 @@ clutter_backend_egl_post_parse (ClutterBackend  *backend,
 			      &backend_egl->egl_version_major,
 			      &backend_egl->egl_version_minor);
 
+      g_atexit (clutter_backend_at_exit);
+
       if (status != EGL_TRUE)
 	{
 	  g_set_error (error, CLUTTER_INIT_ERROR,