Index: gdk/gdkpango.c
===================================================================
RCS file: /cvs/gnome/gtk+/gdk/gdkpango.c,v
retrieving revision 1.32.2.2
diff -u -p -r1.32.2.2 gdkpango.c
--- gdk/gdkpango.c	7 Apr 2005 05:38:05 -0000	1.32.2.2
+++ gdk/gdkpango.c	6 May 2005 22:09:22 -0000
@@ -603,6 +603,7 @@ gdk_pango_renderer_new (GdkScreen *scree
 
 static void
 on_renderer_display_closed (GdkDisplay       *display,
+                            gboolean          is_error,
 			    GdkPangoRenderer *renderer)
 {
   g_signal_handlers_disconnect_by_func (renderer->priv->screen,
Index: gdk/gdkwindow.c
===================================================================
RCS file: /cvs/gnome/gtk+/gdk/gdkwindow.c,v
retrieving revision 1.165.2.1
diff -u -p -r1.165.2.1 gdkwindow.c
--- gdk/gdkwindow.c	16 Mar 2005 03:25:06 -0000	1.165.2.1
+++ gdk/gdkwindow.c	6 May 2005 22:09:24 -0000
@@ -335,6 +335,7 @@ _gdk_window_destroy_hierarchy (GdkWindow
   GdkWindowObject *private;
   GdkWindowObject *temp_private;
   GdkWindow *temp_window;
+  GdkScreen *screen;
   GList *children;
   GList *tmp;
   
@@ -347,6 +348,14 @@ _gdk_window_destroy_hierarchy (GdkWindow
     
   switch (GDK_WINDOW_TYPE (window))
     {
+    case GDK_WINDOW_ROOT:
+      screen = gdk_drawable_get_screen (GDK_DRAWABLE (window));
+      if (!screen->closed)
+	{
+	  g_error ("attempted to destroy root window");
+	  break;
+	}
+      /* else fall thru */
     case GDK_WINDOW_TOPLEVEL:
     case GDK_WINDOW_CHILD:
     case GDK_WINDOW_DIALOG:
@@ -421,10 +430,6 @@ _gdk_window_destroy_hierarchy (GdkWindow
 
           gdk_drawable_set_colormap (GDK_DRAWABLE (window), NULL);
 	}
-      break;
-      
-    case GDK_WINDOW_ROOT:
-      g_error ("attempted to destroy root window");
       break;
     }
 }
Index: gdk/x11/gdkdisplay-x11.c
===================================================================
RCS file: /cvs/gnome/gtk+/gdk/x11/gdkdisplay-x11.c,v
retrieving revision 1.51.2.6
diff -u -p -r1.51.2.6 gdkdisplay-x11.c
--- gdk/x11/gdkdisplay-x11.c	16 Mar 2005 03:25:09 -0000	1.51.2.6
+++ gdk/x11/gdkdisplay-x11.c	6 May 2005 22:09:24 -0000
@@ -697,18 +697,20 @@ gdk_x11_display_ungrab (GdkDisplay * dis
 static void
 gdk_display_x11_dispose (GObject *object)
 {
-  GdkDisplayX11 *display_x11;
-  gint i;
-  
-  display_x11 = GDK_DISPLAY_X11 (object);
-  
-  for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
-    _gdk_screen_close (display_x11->screens[i]);
+  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
+  gint           n_screens;
+  gint           i;
 
-  g_source_destroy (display_x11->event_source);
+  n_screens = ScreenCount (display_x11->xdisplay);
 
-  XCloseDisplay (display_x11->xdisplay);
-  display_x11->xdisplay = NULL;
+  for (i = 0; i < n_screens; i++)
+    _gdk_screen_close (display_x11->screens[i]);
+
+  if (display_x11->event_source)
+    {
+      g_source_destroy (display_x11->event_source);
+      display_x11->event_source = NULL;
+    }
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
@@ -717,11 +719,13 @@ static void
 gdk_display_x11_finalize (GObject *object)
 {
   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
-  int i;
-  GList *tmp;
-  /* FIXME need to write GdkKeymap finalize fct 
-     g_object_unref (display_x11->keymap);
-   */
+  gint           n_screens;
+  gint           i;
+
+  /* Keymap */
+  if (display_x11->keymap)
+    g_object_unref (display_x11->keymap);
+
   /* Free motif Dnd */
   if (display_x11->motif_target_lists)
     {
@@ -733,30 +737,40 @@ gdk_display_x11_finalize (GObject *objec
   /* Atom Hashtable */
   g_hash_table_destroy (display_x11->atom_from_virtual);
   g_hash_table_destroy (display_x11->atom_to_virtual);
+
   /* Leader Window */
   XDestroyWindow (display_x11->xdisplay, display_x11->leader_window);
+
   /* list of filters for client messages */
+  g_list_foreach (display_x11->client_filters, (GFunc) g_free, NULL);
   g_list_free (display_x11->client_filters);
+
   /* List of event window extraction functions */
   g_slist_foreach (display_x11->event_types, (GFunc)g_free, NULL);
   g_slist_free (display_x11->event_types);
+
   /* X ID hashtable */
   g_hash_table_destroy (display_x11->xid_ht);
+
   /* input GdkDevice list */
   /* FIXME need to write finalize fct */
-  for (tmp = display_x11->input_devices; tmp; tmp = tmp->next)
-    g_object_unref (tmp->data);
+  g_list_foreach (display_x11->input_devices, (GFunc) g_object_unref, NULL);
   g_list_free (display_x11->input_devices);
+
   /* input GdkWindow list */
-  for (tmp = display_x11->input_windows; tmp; tmp = tmp->next)
-    g_object_unref (tmp->data);
+  g_list_foreach (display_x11->input_windows, (GFunc) g_object_unref, NULL);
   g_list_free (display_x11->input_windows);
+
   /* Free all GdkScreens */
+  n_screens = ScreenCount (display_x11->xdisplay);
   for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
     g_object_unref (display_x11->screens[i]);
   g_free (display_x11->screens);
+
   g_free (display_x11->startup_notification_id);
-  
+
+  XCloseDisplay (display_x11->xdisplay);
+
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
@@ -832,10 +846,13 @@ _gdk_windowing_set_default_display (GdkD
   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
   const gchar *startup_id;
   
-  if (display)
-    gdk_display = GDK_DISPLAY_XDISPLAY (display);
-  else
-    gdk_display = NULL;
+  if (!display)
+    {
+      gdk_display = NULL;
+      return;
+    }
+
+  gdk_display = GDK_DISPLAY_XDISPLAY (display);
 
   g_free (display_x11->startup_notification_id);
   display_x11->startup_notification_id = NULL;
Index: gdk/x11/gdkevents-x11.c
===================================================================
RCS file: /cvs/gnome/gtk+/gdk/x11/gdkevents-x11.c,v
retrieving revision 1.144.2.3
diff -u -p -r1.144.2.3 gdkevents-x11.c
--- gdk/x11/gdkevents-x11.c	5 May 2005 00:09:42 -0000	1.144.2.3
+++ gdk/x11/gdkevents-x11.c	6 May 2005 22:09:26 -0000
@@ -193,8 +193,11 @@ _gdk_x11_events_uninit_screen (GdkScreen
 {
   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
 
-  xsettings_client_destroy (screen_x11->xsettings_client);
-  screen_x11->xsettings_client = NULL;
+  if (screen_x11->xsettings_client)
+    {
+      xsettings_client_destroy (screen_x11->xsettings_client);
+      screen_x11->xsettings_client = NULL;
+    }
 }
 
 void 
Index: gdk/x11/gdkkeys-x11.c
===================================================================
RCS file: /cvs/gnome/gtk+/gdk/x11/gdkkeys-x11.c,v
retrieving revision 1.44.2.2
diff -u -p -r1.44.2.2 gdkkeys-x11.c
--- gdk/x11/gdkkeys-x11.c	16 Mar 2005 03:25:09 -0000	1.44.2.2
+++ gdk/x11/gdkkeys-x11.c	6 May 2005 22:09:26 -0000
@@ -53,7 +53,8 @@
 #  endif
 #endif /* HAVE_XKB */
 
-typedef struct _GdkKeymapX11 GdkKeymapX11;
+typedef struct _GdkKeymapX11   GdkKeymapX11;
+typedef struct _GdkKeymapClass GdkKeymapX11Class;
 
 #define GDK_TYPE_KEYMAP_X11          (gdk_keymap_x11_get_type ())
 #define GDK_KEYMAP_X11(object)       (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_KEYMAP_X11, GdkKeymapX11))
@@ -102,8 +103,12 @@ struct _GdkKeymapX11
 #define KEYMAP_USE_XKB(keymap) GDK_DISPLAY_X11 ((keymap)->display)->use_xkb
 #define KEYMAP_XDISPLAY(keymap) GDK_DISPLAY_XDISPLAY ((keymap)->display)
 
-static GType gdk_keymap_x11_get_type (void);
-static void  gdk_keymap_x11_init     (GdkKeymapX11 *keymap);
+static GType gdk_keymap_x11_get_type   (void);
+static void  gdk_keymap_x11_class_init (GdkKeymapX11Class *klass);
+static void  gdk_keymap_x11_init       (GdkKeymapX11      *keymap);
+static void  gdk_keymap_x11_finalize   (GObject           *object);
+
+static GdkKeymapClass *parent_class = NULL;
 
 static GType
 gdk_keymap_x11_get_type (void)
@@ -117,7 +122,7 @@ gdk_keymap_x11_get_type (void)
 	  sizeof (GdkKeymapClass),
 	  (GBaseInitFunc) NULL,
 	  (GBaseFinalizeFunc) NULL,
-	  (GClassInitFunc) NULL,
+	  (GClassInitFunc) gdk_keymap_x11_class_init,
 	  NULL,           /* class_finalize */
 	  NULL,           /* class_data */
 	  sizeof (GdkKeymapX11),
@@ -134,6 +139,16 @@ gdk_keymap_x11_get_type (void)
 }
 
 static void
+gdk_keymap_x11_class_init (GdkKeymapX11Class *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  parent_class = g_type_class_peek_parent (klass);
+
+  object_class->finalize = gdk_keymap_x11_finalize;
+}
+
+static void
 gdk_keymap_x11_init (GdkKeymapX11 *keymap)
 {
   keymap->min_keycode = 0;
@@ -156,6 +171,25 @@ gdk_keymap_x11_init (GdkKeymapX11 *keyma
   keymap->current_cache_serial = 0;
 #endif
 
+}
+
+static void
+gdk_keymap_x11_finalize (GObject *object)
+{
+  GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (object);
+
+  if (keymap_x11->keymap)
+    XFree (keymap_x11->keymap);
+
+  if (keymap_x11->mod_keymap)
+    XFreeModifiermap (keymap_x11->mod_keymap);
+
+#ifdef HAVE_XKB
+  if (keymap_x11->xkb_desc)
+    XkbFreeClientMap (keymap_x11->xkb_desc, 0, True);
+#endif
+
+  G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
 static inline void
Index: gdk/x11/gdkscreen-x11.c
===================================================================
RCS file: /cvs/gnome/gtk+/gdk/x11/gdkscreen-x11.c,v
retrieving revision 1.37.2.2
diff -u -p -r1.37.2.2 gdkscreen-x11.c
--- gdk/x11/gdkscreen-x11.c	16 Mar 2005 03:25:09 -0000	1.37.2.2
+++ gdk/x11/gdkscreen-x11.c	6 May 2005 22:09:27 -0000
@@ -286,10 +286,20 @@ gdk_screen_x11_dispose (GObject *object)
 
   _gdk_x11_events_uninit_screen (GDK_SCREEN (object));
 
-  g_object_unref (screen_x11->default_colormap);
-  screen_x11->default_colormap = NULL;
-  
-  screen_x11->root_window = NULL;
+  if (screen_x11->default_colormap)
+    {
+      g_object_unref (screen_x11->default_colormap);
+      screen_x11->default_colormap = NULL;
+    }
+
+  if (screen_x11->system_colormap)
+    {
+      g_object_unref (screen_x11->system_colormap);
+      screen_x11->system_colormap = NULL;
+    }
+
+  if (screen_x11->root_window)
+    _gdk_window_destroy (screen_x11->root_window, TRUE);
 
   screen_x11->xdisplay = NULL;
   screen_x11->xscreen = NULL;
@@ -304,26 +314,26 @@ static void
 gdk_screen_x11_finalize (GObject *object)
 {
   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (object);
-  /* int i; */
-  g_object_unref (screen_x11->root_window);
+  gint          i;
+
+  if (screen_x11->root_window)
+    g_object_unref (screen_x11->root_window);
 
   if (screen_x11->renderer)
     g_object_unref (screen_x11->renderer);
-  
-  /* Visual Part (Need to implement finalize for Visuals for a clean
-   * finalize) */
-  /* for (i=0;i<screen_x11->nvisuals;i++)
-    g_object_unref (screen_x11->visuals[i]);*/
+
+  /* Visual Part */
+  for (i = 0;i < screen_x11->nvisuals; i++)
+    g_object_unref (screen_x11->visuals[i]);
   g_free (screen_x11->visuals);
   g_hash_table_destroy (screen_x11->visual_hash);
 
   g_free (screen_x11->window_manager_name);  
 
   g_hash_table_destroy (screen_x11->colormap_hash);
-  /* X settings */
-  g_free (screen_x11->xsettings_client);
+
   g_free (screen_x11->monitors);
-  
+
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
Index: gdk/x11/gdkvisual-x11.c
===================================================================
RCS file: /cvs/gnome/gtk+/gdk/x11/gdkvisual-x11.c,v
retrieving revision 1.36.2.2
diff -u -p -r1.36.2.2 gdkvisual-x11.c
--- gdk/x11/gdkvisual-x11.c	16 Mar 2005 03:25:09 -0000	1.36.2.2
+++ gdk/x11/gdkvisual-x11.c	6 May 2005 22:09:27 -0000
@@ -63,19 +63,6 @@ static const gchar *const visual_names[]
 
 #endif /* G_ENABLE_DEBUG */
 
-static void
-gdk_visual_finalize (GObject *object)
-{
-  g_error ("A GdkVisual object was finalized. This should not happen");
-}
-
-static void
-gdk_visual_class_init (GObjectClass *class)
-{
-  class->finalize = gdk_visual_finalize;
-}
-
-
 GType
 gdk_visual_get_type (void)
 {
@@ -88,7 +75,7 @@ gdk_visual_get_type (void)
         sizeof (GdkVisualClass),
         (GBaseInitFunc) NULL,
         (GBaseFinalizeFunc) NULL,
-        (GClassInitFunc) gdk_visual_class_init,
+        (GClassInitFunc) NULL,
         NULL,           /* class_finalize */
         NULL,           /* class_data */
         sizeof (GdkVisualPrivate),
@@ -594,7 +581,7 @@ gdk_visual_add (GdkVisual *visual)
   
   if (!screen_x11->visual_hash)
     screen_x11->visual_hash = g_hash_table_new ((GHashFunc) gdk_visual_hash,
-						 (GEqualFunc) gdk_visual_equal);
+                                                (GEqualFunc) gdk_visual_equal);
 
   g_hash_table_insert (screen_x11->visual_hash, private->xvisual, visual);
 }
