Remove the window from the events loop on destruction

This commit is contained in:
Pierre Krieger 2017-01-28 15:14:23 +01:00
parent 0242daa242
commit d86fdb48d1

View file

@ -56,6 +56,7 @@ macro_rules! gen_api_transition {
pub struct Window2 { pub struct Window2 {
window: ::std::sync::Arc<Window>, window: ::std::sync::Arc<Window>,
events_loop: ::std::sync::Weak<EventsLoop>,
} }
impl ::std::ops::Deref for Window2 { impl ::std::ops::Deref for Window2 {
@ -75,6 +76,7 @@ macro_rules! gen_api_transition {
events_loop.windows.lock().unwrap().push(win.clone()); events_loop.windows.lock().unwrap().push(win.clone());
Ok(Window2 { Ok(Window2 {
window: win, window: win,
events_loop: ::std::sync::Arc::downgrade(&events_loop),
}) })
} }
@ -83,5 +85,14 @@ macro_rules! gen_api_transition {
&*self.window as *const Window as usize &*self.window as *const Window as usize
} }
} }
impl Drop for Window2 {
fn drop(&mut self) {
if let Some(ev) = self.events_loop.upgrade() {
let mut windows = ev.windows.lock().unwrap();
windows.retain(|w| &**w as *const Window != &*self.window as *const _);
}
}
}
}; };
} }