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	29 Apr 2005 13:07: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;
+
+  /* FIXME need to write GdkKeymap finalize fct */
+  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);
 }
 
@@ -836,6 +850,9 @@ _gdk_windowing_set_default_display (GdkD
     gdk_display = GDK_DISPLAY_XDISPLAY (display);
   else
     gdk_display = NULL;
+
+  if (! gdk_display)
+    return;
 
   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.2
diff -u -p -r1.144.2.2 gdkevents-x11.c
--- gdk/x11/gdkevents-x11.c	16 Mar 2005 03:25:09 -0000	1.144.2.2
+++ gdk/x11/gdkevents-x11.c	29 Apr 2005 13:07:25 -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/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	29 Apr 2005 13:07:25 -0000
@@ -286,10 +286,31 @@ 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)
+    {
+      GdkWindowObject *private;
+
+      /* hack badly to be able to unref the root window object */
+      private = (GdkWindowObject *)screen_x11->root_window;
+      private->window_type = GDK_WINDOW_TOPLEVEL;
+
+      _gdk_window_destroy (screen_x11->root_window, TRUE);
+
+      g_object_unref (screen_x11->root_window);
+      screen_x11->root_window = NULL;
+    }
 
   screen_x11->xdisplay = NULL;
   screen_x11->xscreen = NULL;
@@ -305,7 +326,6 @@ gdk_screen_x11_finalize (GObject *object
 {
   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (object);
   /* int i; */
-  g_object_unref (screen_x11->root_window);
 
   if (screen_x11->renderer)
     g_object_unref (screen_x11->renderer);
@@ -320,10 +340,9 @@ gdk_screen_x11_finalize (GObject *object
   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);
 }
 
