From 476eea58a4ef674acfbbfb6ed404af7ab7161a9e Mon Sep 17 00:00:00 2001 From: archibate <17721388340@163.com> Date: Sat, 6 Jun 2020 12:56:12 +0800 Subject: [PATCH] [skip ci] RAII wmDeleteMessage --- taichi/gui/gui.h | 2 +- taichi/gui/x11.cpp | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/taichi/gui/gui.h b/taichi/gui/gui.h index 1cc378e82c803..76b4406ebd3d4 100644 --- a/taichi/gui/gui.h +++ b/taichi/gui/gui.h @@ -428,7 +428,7 @@ class GUIBaseX11 { void *visual; unsigned long window; CXImage *img; - void *wmDeleteMessage; + std::vector wmDeleteMessage; }; using GUIBase = GUIBaseX11; diff --git a/taichi/gui/x11.cpp b/taichi/gui/x11.cpp index b3e3ac69faff2..6b735c334c720 100644 --- a/taichi/gui/x11.cpp +++ b/taichi/gui/x11.cpp @@ -76,7 +76,7 @@ void GUI::process_event() { break; case ClientMessage: // https://stackoverflow.com/questions/10792361/how-do-i-gracefully-exit-an-x11-event-loop - if (ev.xclient.data.l[0] == *(Atom *)wmDeleteMessage) { + if (ev.xclient.data.l[0] == *(Atom *)wmDeleteMessage.data()) { key_events.push_back( KeyEvent{KeyEvent::Type::press, "WMClose", cursor_pos}); should_close++; @@ -123,10 +123,10 @@ void GUI::create_window() { ButtonPressMask | ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPress | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask); - wmDeleteMessage = std::malloc(sizeof(Atom)); - *(Atom *)wmDeleteMessage = + wmDeleteMessage = std::vector(sizeof(Atom)); + *(Atom *)wmDeleteMessage.data() = XInternAtom((Display *)display, "WM_DELETE_WINDOW", False); - XSetWMProtocols((Display *)display, window, (Atom *)wmDeleteMessage, 1); + XSetWMProtocols((Display *)display, window, (Atom *)wmDeleteMessage.data(), 1); XMapWindow((Display *)display, window); img = new CXImage((Display *)display, (Visual *)visual, width, height); } @@ -143,7 +143,6 @@ void GUI::set_title(std::string title) { GUI::~GUI() { XCloseDisplay((Display *)display); - std::free(wmDeleteMessage); delete img; }