commit df5eb5e

Michael Forney  ·  2013-11-30 07:43:50 +0000 UTC
parent 50b8225
Improve window surface destruction handling

Now, the free() is in the same file as the malloc(), and the shell
surface isn't destroyed until it's resource is destroyed.
2 files changed,  +23, -2
+22, -1
 1@@ -34,6 +34,7 @@ struct swc_shell_surface
 2     struct swc_window_internal window;
 3 
 4     struct wl_resource * resource;
 5+    struct wl_listener surface_destroy_listener;
 6 
 7     enum
 8     {
 9@@ -169,6 +170,22 @@ static const struct swc_window_impl shell_window_impl = {
10     .configure = &configure
11 };
12 
13+static void handle_surface_destroy(struct wl_listener * listener, void * data)
14+{
15+    struct swc_shell_surface * shell_surface = CONTAINER_OF
16+        (listener, typeof(*shell_surface), surface_destroy_listener);
17+
18+    wl_resource_destroy(shell_surface->resource);
19+}
20+
21+static void destroy_shell_surface(struct wl_resource * resource)
22+{
23+    struct swc_shell_surface * shell_surface
24+        = wl_resource_get_user_data(resource);
25+
26+    free(shell_surface);
27+}
28+
29 struct swc_shell_surface * swc_shell_surface_new
30     (struct wl_client * client, uint32_t id, struct swc_surface * surface)
31 {
32@@ -180,6 +197,10 @@ struct swc_shell_surface * swc_shell_surface_new
33         goto error0;
34 
35     shell_surface->type = SHELL_SURFACE_TYPE_UNSPECIFIED;
36+    shell_surface->surface_destroy_listener.notify = &handle_surface_destroy;
37+    wl_resource_add_destroy_listener(surface->resource,
38+                                     &shell_surface->surface_destroy_listener);
39+
40     swc_window_initialize(&shell_surface->window.base,
41                           &shell_window_impl, surface);
42 
43@@ -191,7 +212,7 @@ struct swc_shell_surface * swc_shell_surface_new
44 
45     wl_resource_set_implementation(shell_surface->resource,
46                                    &shell_surface_implementation,
47-                                   shell_surface, NULL);
48+                                   shell_surface, &destroy_shell_surface);
49 
50     return shell_surface;
51 
+1, -1
1@@ -102,7 +102,7 @@ static void handle_surface_destroy(struct wl_listener * listener, void * data)
2         (listener, struct swc_window_internal, surface_destroy_listener)->base;
3 
4     swc_send_event(&window->event_signal, SWC_WINDOW_DESTROYED, NULL);
5-    free(window);
6+    swc_surface_set_class(INTERNAL(window)->surface, NULL);
7 }
8 
9 EXPORT