diff --git a/src/gl/x11.rs b/src/gl/x11.rs index b7aaca47..4a4de735 100644 --- a/src/gl/x11.rs +++ b/src/gl/x11.rs @@ -241,5 +241,9 @@ impl GlContext { } impl Drop for GlContext { - fn drop(&mut self) {} + fn drop(&mut self) { + unsafe { + glx::glXDestroyContext(self.display, self.context); + } + } } diff --git a/src/x11/window.rs b/src/x11/window.rs index 5b801eca..eda3260f 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -31,19 +31,13 @@ use crate::x11::visual_info::WindowVisualConfig; pub struct WindowHandle { raw_window_handle: Option, - close_requested: Arc, + close_requested: mpsc::SyncSender<()>, is_open: Arc, } impl WindowHandle { pub fn close(&mut self) { - if self.raw_window_handle.take().is_some() { - // FIXME: This will need to be changed from just setting an atomic to somehow - // synchronizing with the window being closed (using a synchronous channel, or - // by joining on the event loop thread). - - self.close_requested.store(true, Ordering::Relaxed); - } + self.close_requested.send(()).ok(); } pub fn is_open(&self) -> bool { @@ -64,26 +58,26 @@ unsafe impl HasRawWindowHandle for WindowHandle { } pub(crate) struct ParentHandle { - close_requested: Arc, + close_requested: mpsc::Receiver<()>, is_open: Arc, } impl ParentHandle { pub fn new() -> (Self, WindowHandle) { - let close_requested = Arc::new(AtomicBool::new(false)); + let (close_sender, close_receiver) = mpsc::sync_channel(0); let is_open = Arc::new(AtomicBool::new(true)); let handle = WindowHandle { raw_window_handle: None, - close_requested: Arc::clone(&close_requested), + close_requested: close_sender, is_open: Arc::clone(&is_open), }; - (Self { close_requested, is_open }, handle) + (Self { close_requested: close_receiver, is_open }, handle) } pub fn parent_did_drop(&self) -> bool { - self.close_requested.load(Ordering::Relaxed) + self.close_requested.try_recv().is_ok() } } @@ -94,6 +88,10 @@ impl Drop for ParentHandle { } pub(crate) struct WindowInner { + // GlContext should be dropped **before** XcbConnection is dropped + #[cfg(feature = "opengl")] + gl_context: Option, + pub(crate) xcb_connection: XcbConnection, window_id: XWindow, pub(crate) window_info: WindowInfo, @@ -101,9 +99,6 @@ pub(crate) struct WindowInner { mouse_cursor: Cell, pub(crate) close_requested: Cell, - - #[cfg(feature = "opengl")] - gl_context: Option, } pub struct Window<'a> {