From: Mikko Rasa Date: Fri, 11 Dec 2015 16:47:59 +0000 (+0200) Subject: Fix the shutdown sequence to avoid memory leaks X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=cad63ae5d592e02bcea6edc00205853761b5289e;p=geometrycompositor.git Fix the shutdown sequence to avoid memory leaks --- diff --git a/source/main.c b/source/main.c index 15486e6..05b5955 100644 --- a/source/main.c +++ b/source/main.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -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; inwindows; ++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; inscreens; ++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);