diff --git a/hw/xwayland/xwayland-window.c b/hw/xwayland/xwayland-window.c
index 919c0a0ca92dcdd17a8d35122f0376af7ea43a73..2c6b74fce6071a4face94dcbc444072dd445ca6d 100644
--- a/hw/xwayland/xwayland-window.c
+++ b/hw/xwayland/xwayland-window.c
@@ -483,14 +483,19 @@ ensure_surface_for_window(WindowPtr window)
             goto err_surf;
         }
 
+        xwl_window->xdg_toplevel =
+            xdg_surface_get_toplevel(xwl_window->xdg_surface);
+        if (xwl_window->xdg_surface == NULL) {
+            ErrorF("Failed creating xdg_toplevel\n");
+            goto err_surf;
+        }
+
         wl_surface_add_listener(xwl_window->surface,
                                 &surface_listener, xwl_window);
 
         xdg_surface_add_listener(xwl_window->xdg_surface,
                                  &xdg_surface_listener, xwl_window);
 
-        xdg_surface_get_toplevel(xwl_window->xdg_surface);
-
         wl_surface_commit(xwl_window->surface);
 
         region = wl_compositor_create_region(xwl_screen->compositor);
@@ -540,6 +545,8 @@ ensure_surface_for_window(WindowPtr window)
     return TRUE;
 
 err_surf:
+    if (xwl_window->xdg_toplevel)
+        xdg_toplevel_destroy(xwl_window->xdg_toplevel);
     if (xwl_window->xdg_surface)
         xdg_surface_destroy(xwl_window->xdg_surface);
     wl_surface_destroy(xwl_window->surface);
diff --git a/hw/xwayland/xwayland-window.h b/hw/xwayland/xwayland-window.h
index 9739af17c5d5bde66faa005ff069f5ce70b70b07..1975b749c0f6c41d652d2ae1178e623a75983164 100644
--- a/hw/xwayland/xwayland-window.h
+++ b/hw/xwayland/xwayland-window.h
@@ -43,6 +43,7 @@ struct xwl_window {
     struct wp_viewport *viewport;
     float scale_x, scale_y;
     struct xdg_surface *xdg_surface;
+    struct xdg_toplevel *xdg_toplevel;
     WindowPtr window;
     struct xorg_list link_damage;
     struct xorg_list link_window;