diff --git a/gtk/gtkbutton.c b/gtk/gtkbutton.c
index d86ca46..9392b11 100644
--- a/gtk/gtkbutton.c
+++ b/gtk/gtkbutton.c
@@ -1300,8 +1300,7 @@ gtk_button_realize (GtkWidget *widget)
   attributes_mask = GDK_WA_X | GDK_WA_Y;
 
   window = gtk_widget_get_parent_window (widget);
-  gtk_widget_set_window (widget, window);
-  g_object_ref (window);
+  gtk_widget_attach_window (widget, window);
 
   priv->event_window = gdk_window_new (window,
                                        &attributes, attributes_mask);
diff --git a/gtk/gtkdrawingarea.c b/gtk/gtkdrawingarea.c
index 6f8272b..ef5eab3 100644
--- a/gtk/gtkdrawingarea.c
+++ b/gtk/gtkdrawingarea.c
@@ -166,7 +166,6 @@ gtk_drawing_area_new (void)
 static void
 gtk_drawing_area_realize (GtkWidget *widget)
 {
-  GtkDrawingArea *darea = GTK_DRAWING_AREA (widget);
   GtkAllocation allocation;
   GdkWindow *window;
   GdkWindowAttr attributes;
@@ -195,7 +194,6 @@ gtk_drawing_area_realize (GtkWidget *widget)
 
       window = gdk_window_new (gtk_widget_get_parent_window (widget),
                                &attributes, attributes_mask);
-      gdk_window_set_user_data (window, darea);
       gtk_widget_set_window (widget, window);
 
       gtk_style_context_set_background (gtk_widget_get_style_context (widget),
diff --git a/gtk/gtkentry.c b/gtk/gtkentry.c
index e7bd3d9..98420a9 100644
--- a/gtk/gtkentry.c
+++ b/gtk/gtkentry.c
@@ -2825,8 +2825,7 @@ gtk_entry_realize (GtkWidget *widget)
 
   gtk_widget_set_realized (widget, TRUE);
   window = gtk_widget_get_parent_window (widget);
-  gtk_widget_set_window (widget, window);
-  g_object_ref (window);
+  gtk_widget_attach_window (widget, window);
 
   entry = GTK_ENTRY (widget);
   priv = entry->priv;
diff --git a/gtk/gtkeventbox.c b/gtk/gtkeventbox.c
index b8492c1..9756a4a 100644
--- a/gtk/gtkeventbox.c
+++ b/gtk/gtkeventbox.c
@@ -430,14 +430,13 @@ gtk_event_box_realize (GtkWidget *widget)
 
       window = gdk_window_new (gtk_widget_get_parent_window (widget),
                                &attributes, attributes_mask);
-      gtk_widget_set_window (widget, window);
-      gdk_window_set_user_data (window, widget);
+      gtk_widget_attach_window (widget, window);
+      g_object_unref (window);
     }
   else
     {
       window = gtk_widget_get_parent_window (widget);
-      gtk_widget_set_window (widget, window);
-      g_object_ref (window);
+      gtk_widget_attach_window (widget, window);
     }
 
   if (!visible_window || priv->above_child)
diff --git a/gtk/gtkexpander.c b/gtk/gtkexpander.c
index 0370c12..0298ca2 100644
--- a/gtk/gtkexpander.c
+++ b/gtk/gtkexpander.c
@@ -546,8 +546,7 @@ gtk_expander_realize (GtkWidget *widget)
   attributes_mask = GDK_WA_X | GDK_WA_Y;
 
   window = gtk_widget_get_parent_window (widget);
-  gtk_widget_set_window (widget, window);
-  g_object_ref (window);
+  gtk_widget_attach_window (widget, window);
 
   priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
                                        &attributes, attributes_mask);
diff --git a/gtk/gtkfixed.c b/gtk/gtkfixed.c
index d75f599..23d0f9b 100644
--- a/gtk/gtkfixed.c
+++ b/gtk/gtkfixed.c
@@ -306,8 +306,8 @@ gtk_fixed_realize (GtkWidget *widget)
 
       window = gdk_window_new (gtk_widget_get_parent_window (widget),
                                &attributes, attributes_mask);
-      gtk_widget_set_window (widget, window);
-      gdk_window_set_user_data (window, widget);
+      gtk_widget_attach_window (widget, window);
+      g_object_unref (window);
 
       gtk_style_context_set_background (gtk_widget_get_style_context (widget),
                                         window);
diff --git a/gtk/gtkhandlebox.c b/gtk/gtkhandlebox.c
index a57d81b..4ef8e3e 100644
--- a/gtk/gtkhandlebox.c
+++ b/gtk/gtkhandlebox.c
@@ -438,8 +438,8 @@ gtk_handle_box_realize (GtkWidget *widget)
 
   window = gdk_window_new (gtk_widget_get_parent_window (widget),
                            &attributes, attributes_mask);
-  gtk_widget_set_window (widget, window);
-  gdk_window_set_user_data (window, widget);
+  gtk_widget_attach_window (widget, window);
+  g_object_unref (window);
 
   attributes.x = 0;
   attributes.y = 0;
diff --git a/gtk/gtkhsv.c b/gtk/gtkhsv.c
index 62356bc..3275a07 100644
--- a/gtk/gtkhsv.c
+++ b/gtk/gtkhsv.c
@@ -247,8 +247,7 @@ gtk_hsv_realize (GtkWidget *widget)
   attr_mask = GDK_WA_X | GDK_WA_Y;
 
   parent_window = gtk_widget_get_parent_window (widget);
-  gtk_widget_set_window (widget, parent_window);
-  g_object_ref (parent_window);
+  gtk_widget_attach_window (widget, parent_window);
 
   priv->window = gdk_window_new (parent_window, &attr, attr_mask);
   gdk_window_set_user_data (priv->window, hsv);
diff --git a/gtk/gtkiconview.c b/gtk/gtkiconview.c
index 47dc4c7..27c8f13 100644
--- a/gtk/gtkiconview.c
+++ b/gtk/gtkiconview.c
@@ -1412,8 +1412,8 @@ gtk_icon_view_realize (GtkWidget *widget)
 
   window = gdk_window_new (gtk_widget_get_parent_window (widget),
                            &attributes, attributes_mask);
-  gtk_widget_set_window (widget, window);
-  gdk_window_set_user_data (window, widget);
+  gtk_widget_attach_window (widget, window);
+  g_object_unref (window);
 
   gtk_widget_get_allocation (widget, &allocation);
 
diff --git a/gtk/gtkinvisible.c b/gtk/gtkinvisible.c
index 7003a18..54b3109 100644
--- a/gtk/gtkinvisible.c
+++ b/gtk/gtkinvisible.c
@@ -247,8 +247,8 @@ gtk_invisible_realize (GtkWidget *widget)
   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
 
   window = gdk_window_new (parent, &attributes, attributes_mask);
-  gtk_widget_set_window (widget, window);
-  gdk_window_set_user_data (window, widget);
+  gtk_widget_attach_window (widget, window);
+  g_object_unref (window);
 }
 
 static void
diff --git a/gtk/gtklayout.c b/gtk/gtklayout.c
index 3cb0a82..1db2321 100644
--- a/gtk/gtklayout.c
+++ b/gtk/gtklayout.c
@@ -872,8 +872,8 @@ gtk_layout_realize (GtkWidget *widget)
 
   window = gdk_window_new (gtk_widget_get_parent_window (widget),
                            &attributes, attributes_mask);
-  gtk_widget_set_window (widget, window);
-  gdk_window_set_user_data (window, widget);
+  gtk_widget_attach_window (widget, window);
+  g_object_unref (window);
 
   gtk_widget_get_allocation (widget, &allocation);
 
diff --git a/gtk/gtkmenu.c b/gtk/gtkmenu.c
index c8c6c64..29f9d8f 100644
--- a/gtk/gtkmenu.c
+++ b/gtk/gtkmenu.c
@@ -2386,8 +2386,8 @@ gtk_menu_realize (GtkWidget *widget)
 
   window = gdk_window_new (gtk_widget_get_parent_window (widget),
                            &attributes, attributes_mask);
-  gtk_widget_set_window (widget, window);
-  gdk_window_set_user_data (window, widget);
+  gtk_widget_attach_window (widget, window);
+  g_object_unref (window);
 
   get_menu_border (widget, &border);
   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
diff --git a/gtk/gtkmenuitem.c b/gtk/gtkmenuitem.c
index a111db5..5512b0a 100644
--- a/gtk/gtkmenuitem.c
+++ b/gtk/gtkmenuitem.c
@@ -1481,8 +1481,7 @@ gtk_menu_item_realize (GtkWidget *widget)
   gtk_widget_set_realized (widget, TRUE);
 
   window = gtk_widget_get_parent_window (widget);
-  gtk_widget_set_window (widget, window);
-  g_object_ref (window);
+  gtk_widget_attach_window (widget, window);
 
   gtk_widget_get_allocation (widget, &allocation);
 
diff --git a/gtk/gtkmenushell.c b/gtk/gtkmenushell.c
index 1bbbc17..05a11f7 100644
--- a/gtk/gtkmenushell.c
+++ b/gtk/gtkmenushell.c
@@ -529,8 +529,8 @@ gtk_menu_shell_realize (GtkWidget *widget)
 
   window = gdk_window_new (gtk_widget_get_parent_window (widget),
                            &attributes, attributes_mask);
-  gtk_widget_set_window (widget, window);
-  gdk_window_set_user_data (window, widget);
+  gtk_widget_attach_window (widget, window);
+  g_object_unref (window);
 
   context = gtk_widget_get_style_context (widget);
   gtk_style_context_set_background (context, window);
diff --git a/gtk/gtkmisc.c b/gtk/gtkmisc.c
index bc24ae6..65a60d0 100644
--- a/gtk/gtkmisc.c
+++ b/gtk/gtkmisc.c
@@ -336,8 +336,7 @@ gtk_misc_realize (GtkWidget *widget)
   if (!gtk_widget_get_has_window (widget))
     {
       window = gtk_widget_get_parent_window (widget);
-      gtk_widget_set_window (widget, window);
-      g_object_ref (window);
+      gtk_widget_attach_window (widget, window);
     }
   else
     {
@@ -354,8 +353,9 @@ gtk_misc_realize (GtkWidget *widget)
       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
 
       window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
-      gtk_widget_set_window (widget, window);
-      gdk_window_set_user_data (window, widget);
+      gtk_widget_attach_window (widget, window);
       gdk_window_set_background_pattern (window, NULL);
+
+      g_object_unref (window);
     }
 }
diff --git a/gtk/gtknotebook.c b/gtk/gtknotebook.c
index 0d9d9fa..d84d675 100644
--- a/gtk/gtknotebook.c
+++ b/gtk/gtknotebook.c
@@ -1847,8 +1847,7 @@ gtk_notebook_realize (GtkWidget *widget)
   gtk_notebook_get_event_window_position (notebook, &event_window_pos);
 
   window = gtk_widget_get_parent_window (widget);
-  gtk_widget_set_window (widget, window);
-  g_object_ref (window);
+  gtk_widget_attach_window (widget, window);
 
   attributes.window_type = GDK_WINDOW_CHILD;
   attributes.x = event_window_pos.x;
diff --git a/gtk/gtkoffscreenwindow.c b/gtk/gtkoffscreenwindow.c
index 306c318..ebbc434 100644
--- a/gtk/gtkoffscreenwindow.c
+++ b/gtk/gtkoffscreenwindow.c
@@ -186,8 +186,8 @@ gtk_offscreen_window_realize (GtkWidget *widget)
 
   window = gdk_window_new (gtk_widget_get_parent_window (widget),
                            &attributes, attributes_mask);
-  gtk_widget_set_window (widget, window);
-  gdk_window_set_user_data (window, widget);
+  gtk_widget_attach_window (widget, window);
+  g_object_unref (window);
 
   child = gtk_bin_get_child (bin);
   if (child)
diff --git a/gtk/gtkpaned.c b/gtk/gtkpaned.c
index 6962059..c569762 100644
--- a/gtk/gtkpaned.c
+++ b/gtk/gtkpaned.c
@@ -1109,8 +1109,7 @@ gtk_paned_realize (GtkWidget *widget)
   gtk_widget_set_realized (widget, TRUE);
 
   window = gtk_widget_get_parent_window (widget);
-  gtk_widget_set_window (widget, window);
-  g_object_ref (window);
+  gtk_widget_attach_window (widget, window);
 
   attributes.window_type = GDK_WINDOW_CHILD;
   attributes.wclass = GDK_INPUT_ONLY;
diff --git a/gtk/gtkpathbar.c b/gtk/gtkpathbar.c
index 88b89cb..91f8945 100644
--- a/gtk/gtkpathbar.c
+++ b/gtk/gtkpathbar.c
@@ -446,8 +446,7 @@ gtk_path_bar_realize (GtkWidget *widget)
 
   path_bar = GTK_PATH_BAR (widget);
   window = gtk_widget_get_parent_window (widget);
-  gtk_widget_set_window (widget, window);
-  g_object_ref (window);
+  gtk_widget_attach_window (widget, window);
 
   gtk_widget_get_allocation (widget, &allocation);
 
diff --git a/gtk/gtkrange.c b/gtk/gtkrange.c
index 4c8d428..83cfcd7 100644
--- a/gtk/gtkrange.c
+++ b/gtk/gtkrange.c
@@ -1680,8 +1680,7 @@ gtk_range_realize (GtkWidget *widget)
   gtk_widget_set_realized (widget, TRUE);
 
   window = gtk_widget_get_parent_window (widget);
-  gtk_widget_set_window (widget, window);
-  g_object_ref (window);
+  gtk_widget_attach_window (widget, window);
 
   gtk_widget_get_allocation (widget, &allocation);
   if (modify_allocation_for_window_grip (widget, &allocation))
diff --git a/gtk/gtkseparatortoolitem.c b/gtk/gtkseparatortoolitem.c
index be45120..8d6260a 100644
--- a/gtk/gtkseparatortoolitem.c
+++ b/gtk/gtkseparatortoolitem.c
@@ -296,8 +296,7 @@ gtk_separator_tool_item_realize (GtkWidget *widget)
   attributes_mask = GDK_WA_X | GDK_WA_Y;
 
   window = gtk_widget_get_parent_window (widget);
-  gtk_widget_set_window (widget, window);
-  g_object_ref (window);
+  gtk_widget_attach_window (widget, window);
 
   priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
                                        &attributes, attributes_mask);
diff --git a/gtk/gtksocket.c b/gtk/gtksocket.c
index 016c607..0a7fcc3 100644
--- a/gtk/gtksocket.c
+++ b/gtk/gtksocket.c
@@ -387,8 +387,8 @@ gtk_socket_realize (GtkWidget *widget)
 
   window = gdk_window_new (gtk_widget_get_parent_window (widget),
                            &attributes, attributes_mask);
-  gtk_widget_set_window (widget, window);
-  gdk_window_set_user_data (window, socket);
+  gtk_widget_attach_window (widget, window);
+  g_object_unref (window);
 
   gtk_style_context_set_background (gtk_widget_get_style_context (widget),
                                     window);
diff --git a/gtk/gtkswitch.c b/gtk/gtkswitch.c
index 6b9866e..b75df05 100644
--- a/gtk/gtkswitch.c
+++ b/gtk/gtkswitch.c
@@ -404,8 +404,7 @@ gtk_switch_realize (GtkWidget *widget)
 
   gtk_widget_set_realized (widget, TRUE);
   parent_window = gtk_widget_get_parent_window (widget);
-  gtk_widget_set_window (widget, parent_window);
-  g_object_ref (parent_window);
+  gtk_widget_attach_window (widget, parent_window);
 
   gtk_widget_get_allocation (widget, &allocation);
 
diff --git a/gtk/gtktextview.c b/gtk/gtktextview.c
index 5bbd28c..f4cfe4d 100644
--- a/gtk/gtktextview.c
+++ b/gtk/gtktextview.c
@@ -3947,8 +3947,8 @@ gtk_text_view_realize (GtkWidget *widget)
 
   window = gdk_window_new (gtk_widget_get_parent_window (widget),
                            &attributes, attributes_mask);
-  gtk_widget_set_window (widget, window);
-  gdk_window_set_user_data (window, widget);
+  gtk_widget_attach_window (widget, window);
+  g_object_unref (window);
 
   context = gtk_widget_get_style_context (widget);
   state = gtk_widget_get_state_flags (widget);
diff --git a/gtk/gtktoolbar.c b/gtk/gtktoolbar.c
index e222e08..1d87284 100644
--- a/gtk/gtktoolbar.c
+++ b/gtk/gtktoolbar.c
@@ -812,8 +812,7 @@ gtk_toolbar_realize (GtkWidget *widget)
   attributes_mask = GDK_WA_X | GDK_WA_Y;
 
   window = gtk_widget_get_parent_window (widget);
-  gtk_widget_set_window (widget, window);
-  g_object_ref (window);
+  gtk_widget_attach_window (widget, window);
 
   priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
 				       &attributes, attributes_mask);
diff --git a/gtk/gtktoolitem.c b/gtk/gtktoolitem.c
index b296e3b..89dafef 100644
--- a/gtk/gtktoolitem.c
+++ b/gtk/gtktoolitem.c
@@ -446,8 +446,7 @@ gtk_tool_item_realize (GtkWidget *widget)
   gtk_widget_set_realized (widget, TRUE);
 
   window = gtk_widget_get_parent_window (widget);
-  gtk_widget_set_window (widget, window);
-  g_object_ref (window);
+  gtk_widget_attach_window (widget, window);
 
   if (toolitem->priv->use_drag_window)
     create_drag_window(toolitem);
diff --git a/gtk/gtktoolitemgroup.c b/gtk/gtktoolitemgroup.c
index e10e351..0b4fbd8 100644
--- a/gtk/gtktoolitemgroup.c
+++ b/gtk/gtktoolitemgroup.c
@@ -1231,15 +1231,14 @@ gtk_tool_item_group_realize (GtkWidget *widget)
 
   window = gdk_window_new (gtk_widget_get_parent_window (widget),
                            &attributes, attributes_mask);
-  gtk_widget_set_window (widget, window);
+  gtk_widget_attach_window (widget, window);
+  g_object_unref (window);
 
   display = gdk_window_get_display (window);
 
   if (gdk_display_supports_composite (display))
     gdk_window_set_composited (window, TRUE);
 
-  gdk_window_set_user_data (window, widget);
-
   gtk_style_context_set_background (context, window);
 
   gtk_container_forall (GTK_CONTAINER (widget),
diff --git a/gtk/gtktoolpalette.c b/gtk/gtktoolpalette.c
index ed3aed2..52884fa 100644
--- a/gtk/gtktoolpalette.c
+++ b/gtk/gtktoolpalette.c
@@ -757,8 +757,8 @@ gtk_tool_palette_realize (GtkWidget *widget)
 
   window = gdk_window_new (gtk_widget_get_parent_window (widget),
                            &attributes, attributes_mask);
-  gtk_widget_set_window (widget, window);
-  gdk_window_set_user_data (window, widget);
+  gtk_widget_attach_window (widget, window);
+  g_object_unref (window);
 
   gtk_style_context_set_background (gtk_widget_get_style_context (widget),
                                     window);
diff --git a/gtk/gtktreeview.c b/gtk/gtktreeview.c
index 76b748f..2857a8c 100644
--- a/gtk/gtktreeview.c
+++ b/gtk/gtktreeview.c
@@ -2211,8 +2211,8 @@ gtk_tree_view_realize (GtkWidget *widget)
 
   window = gdk_window_new (gtk_widget_get_parent_window (widget),
                            &attributes, attributes_mask);
-  gtk_widget_set_window (widget, window);
-  gdk_window_set_user_data (window, widget);
+  gtk_widget_attach_window (widget, window);
+  g_object_unref (window);
 
   gtk_widget_get_allocation (widget, &allocation);
 
diff --git a/gtk/gtkviewport.c b/gtk/gtkviewport.c
index 09e9fd2..4fc3afa 100644
--- a/gtk/gtkviewport.c
+++ b/gtk/gtkviewport.c
@@ -704,8 +704,8 @@ gtk_viewport_realize (GtkWidget *widget)
 
   window = gdk_window_new (gtk_widget_get_parent_window (widget),
                            &attributes, attributes_mask);
-  gtk_widget_set_window (widget, window);
-  gdk_window_set_user_data (window, viewport);
+  gtk_widget_attach_window (widget, window);
+  g_object_unref (window);
 
   viewport_get_view_allocation (viewport, &view_allocation);
   
diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c
index 45b548a..fa3dfd2 100644
--- a/gtk/gtkwidget.c
+++ b/gtk/gtkwidget.c
@@ -10684,8 +10684,8 @@ gtk_widget_real_realize (GtkWidget *widget)
   gtk_widget_set_realized (widget, TRUE);
   if (priv->parent)
     {
-      priv->window = gtk_widget_get_parent_window (widget);
-      g_object_ref (priv->window);
+      gtk_widget_attach_window (widget, 
+				gtk_widget_get_parent_window (widget));
     }
   priv->style = gtk_style_attach (priv->style, priv->window);
 }
@@ -10720,17 +10720,8 @@ gtk_widget_real_unrealize (GtkWidget *widget)
 			  NULL);
 
   gtk_style_detach (priv->style);
-  if (gtk_widget_get_has_window (widget))
-    {
-      gdk_window_set_user_data (priv->window, NULL);
-      gdk_window_destroy (priv->window);
-      priv->window = NULL;
-    }
-  else
-    {
-      g_object_unref (priv->window);
-      priv->window = NULL;
-    }
+
+  gtk_widget_attach_window (widget, NULL);
 
   gtk_selection_remove_all (widget);
 
@@ -13793,6 +13784,66 @@ gtk_widget_get_requisition (GtkWidget      *widget,
   gtk_widget_get_preferred_size (widget, requisition, NULL);
 }
 
+
+/**
+ * gtk_widget_attach_window:
+ * @widget: a #GtkWidget
+ * @window: a #GdkWindow, or %NULL
+ *
+ * Sets a widget's window. This function should only be used in a
+ * widget's #GtkWidget::realize implementation. The %window passed is
+ * usually either new window created with gdk_window_new(), or the
+ * window of its parent widget as returned by
+ * gtk_widget_get_parent_window().
+ *
+ * Widgets must indicate whether they will create their own #GdkWindow
+ * by calling gtk_widget_set_has_window(). This is usually done in the
+ * widget's init() function.
+ *
+ * Since: 3.0
+ */
+void
+gtk_widget_attach_window (GtkWidget *widget,
+			  GdkWindow *window)
+{
+  GtkWidgetPrivate *priv;
+
+  g_return_if_fail (GTK_IS_WIDGET (widget));
+  g_return_if_fail (window == NULL || GDK_IS_WINDOW (window));
+
+  priv = widget->priv;
+
+  if (priv->window != window)
+    {
+      if (priv->window)
+	{
+	  if (gtk_widget_get_has_window (widget))
+	    {
+	      gdk_window_set_user_data (priv->window, NULL);
+	      gdk_window_destroy (priv->window);
+	      priv->window = NULL;
+	    }
+	  else
+	    {
+	      g_object_unref (priv->window);
+	      priv->window = NULL;
+	    }
+	}
+
+      priv->window = window;
+
+      if (priv->window)
+	{
+	  g_object_ref (priv->window);
+
+	  if (gtk_widget_get_has_window (widget))
+	    gdk_window_set_user_data (priv->window, widget);
+	}
+
+      g_object_notify (G_OBJECT (widget), "window");
+    }
+}
+
 /**
  * gtk_widget_set_window:
  * @widget: a #GtkWidget
@@ -13811,6 +13862,8 @@ gtk_widget_get_requisition (GtkWidget      *widget,
  * <note><para>This function does not add any reference to @window.</para></note>
  *
  * Since: 2.18
+ *
+ * Deprecated: 3.0. Use gtk_widget_attach_window() instead.
  */
 void
 gtk_widget_set_window (GtkWidget *widget,
diff --git a/gtk/gtkwidget.h b/gtk/gtkwidget.h
index 269807f..5dd5545 100644
--- a/gtk/gtkwidget.h
+++ b/gtk/gtkwidget.h
@@ -637,8 +637,8 @@ void                  gtk_widget_set_child_visible      (GtkWidget    *widget,
 							 gboolean      is_visible);
 gboolean              gtk_widget_get_child_visible      (GtkWidget    *widget);
 
-void                  gtk_widget_set_window             (GtkWidget    *widget,
-                                                         GdkWindow    *window);
+void                  gtk_widget_attach_window          (GtkWidget    *widget,
+							 GdkWindow    *window);
 GdkWindow           * gtk_widget_get_window             (GtkWidget    *widget);
 
 int                   gtk_widget_get_allocated_width    (GtkWidget     *widget);
@@ -786,7 +786,10 @@ void         gtk_widget_override_cursor           (GtkWidget       *widget,
 
 #if !defined(GTK_DISABLE_DEPRECATED) || defined(GTK_COMPILATION)
 
-void        gtk_widget_style_attach               (GtkWidget     *widget);
+void        gtk_widget_set_window         (GtkWidget            *widget,
+					   GdkWindow            *window);
+
+void        gtk_widget_style_attach       (GtkWidget            *widget);
 
 /* Widget styles.
  */
diff --git a/gtk/gtkwin32embedwidget.c b/gtk/gtkwin32embedwidget.c
index 623e8d9..02bc7b5 100644
--- a/gtk/gtkwin32embedwidget.c
+++ b/gtk/gtkwin32embedwidget.c
@@ -233,8 +233,8 @@ gtk_win32_embed_widget_realize (GtkWidget *widget)
 
   gdk_window = gdk_window_new (embed_widget->parent_window,
                                &attributes, attributes_mask);
-  gtk_widget_set_window (widget, gdk_window);
-  gdk_window_set_user_data (gdk_window, window);
+  gtk_widget_attach_window (widget, gdk_window);
+  g_object_unref (gdk_window);
 
   embed_widget->old_window_procedure = (gpointer)
     SetWindowLongPtrW(GDK_WINDOW_HWND (gdk_window),
diff --git a/gtk/gtkwindow.c b/gtk/gtkwindow.c
index ca0ad95..f9f7752 100644
--- a/gtk/gtkwindow.c
+++ b/gtk/gtkwindow.c
@@ -4886,8 +4886,8 @@ gtk_window_realize (GtkWidget *widget)
 
       gdk_window = gdk_window_new (gtk_widget_get_parent_window (widget),
 				   &attributes, attributes_mask);
-      gtk_widget_set_window (widget, gdk_window);
-      gdk_window_set_user_data (gdk_window, widget);
+      gtk_widget_attach_window (widget, gdk_window);
+      g_object_unref (gdk_window);
 
       gtk_style_context_set_background (gtk_widget_get_style_context (widget), gdk_window);
 
@@ -4966,15 +4966,14 @@ gtk_window_realize (GtkWidget *widget)
   attributes_mask |= (priv->wmclass_name ? GDK_WA_WMCLASS : 0);
 
   gdk_window = gdk_window_new (parent_window, &attributes, attributes_mask);
-  gtk_widget_set_window (widget, gdk_window);
+  gtk_widget_attach_window (widget, gdk_window);
+  g_object_unref (gdk_window);
 
   if (priv->opacity_set)
     gdk_window_set_opacity (gdk_window, priv->opacity);
 
   gdk_window_enable_synchronized_configure (gdk_window);
 
-  gdk_window_set_user_data (gdk_window, window);
-
   context = gtk_widget_get_style_context (widget);
   gtk_style_context_set_background (context, gdk_window);
 
