]> git.tdb.fi Git - geometrycompositor.git/commitdiff
Fix the shutdown sequence to avoid memory leaks
authorMikko Rasa <tdb@tdb.fi>
Fri, 11 Dec 2015 16:47:59 +0000 (18:47 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 11 Dec 2015 16:47:59 +0000 (18:47 +0200)
source/main.c

index 15486e61c83f3dfef06c88816d1f4db2ade4985b..05b59557495cf9725cee2553db1b3cc5fa1fd2a8 100644 (file)
@@ -2,6 +2,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <signal.h>
 #include <X11/Xlib.h>
 #include <X11/extensions/Xcomposite.h>
 #include <X11/extensions/Xdamage.h>
@@ -87,6 +88,8 @@ static const float vertices[] =
        1.0f, 0.0f
 };
 
+int terminate_requested = 0;
+
 int x_error_handler(Display *display, XErrorEvent *event)
 {
        printf("Ignoring X error %d on resource %lx\n", event->error_code, event->resourceid);
@@ -439,20 +442,40 @@ int initialize_compositor(Compositor *compositor)
 
 void shutdown_screen(Compositor *compositor, CompositedScreen *screen)
 {
+       unsigned i;
+
+       use_gl(compositor, screen);
+
+       for(i=0; i<screen->nwindows; ++i)
+       {
+               glDeleteTextures(1, &screen->windows[i].texture);
+               if(screen->windows[i].pixmap)
+               {
+                       glXDestroyPixmap(compositor->display, screen->windows[i].glx_pixmap);
+                       XFreePixmap(compositor->display, screen->windows[i].pixmap);
+                       XDamageDestroy(compositor->display, screen->windows[i].damage);
+               }
+       }
+
+       glXMakeContextCurrent(compositor->display, 0, 0, NULL);
        glXDestroyContext(compositor->display, screen->glx_context);
        glXDestroyWindow(compositor->display, screen->glx_window);
        XDestroyWindow(compositor->display, screen->render_window);
 
        XCompositeReleaseOverlayWindow(compositor->display, screen->overlay);
+
+       free(screen->windows);
 }
 
 void shutdown_compositor(Compositor *compositor)
 {
        unsigned i;
 
-       glXMakeContextCurrent(compositor->display, 0, 0, NULL);
        for(i=0; i<compositor->nscreens; ++i)
                shutdown_screen(compositor, &compositor->screens[i]);
+       free(compositor->screens);
+
+       XCloseDisplay(compositor->display);
 }
 
 void process_create_window_event(Compositor *compositor, XCreateWindowEvent *event)
@@ -566,14 +589,23 @@ void refresh_screens(Compositor *compositor)
        compositor->dirty = 0;
 }
 
+void sighandler(int sig)
+{
+       terminate_requested = 1;
+       (void)sig;
+}
+
 int main()
 {
        Compositor compositor;
 
+       signal(SIGINT, &sighandler);
+       signal(SIGTERM, &sighandler);
+
        if(!initialize_compositor(&compositor))
                return 1;
 
-       while(1)
+       while(!terminate_requested)
        {
                if(!process_event(&compositor))
                        refresh_screens(&compositor);