From e38b963fa4cf6a8bb1341ba5437f8ed9ebe70730 Mon Sep 17 00:00:00 2001 From: tcmal Date: Fri, 21 Jun 2024 19:25:45 +0100 Subject: avoid leaking resources on drop --- src/conn_info/colours.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src/conn_info/colours.rs') diff --git a/src/conn_info/colours.rs b/src/conn_info/colours.rs index 22b4fc2..d678bdb 100644 --- a/src/conn_info/colours.rs +++ b/src/conn_info/colours.rs @@ -1,7 +1,7 @@ use crate::error::Result; use xcb::{ - x::{AllocColor, Colormap}, - Connection, + x::{AllocColor, Colormap, FreeColors}, + Connection as RawConnection, }; /// Caches colours in an X11 color map. @@ -14,7 +14,7 @@ pub struct Colours { impl Colours { /// Load the colours into the given colour map - pub fn new_with(conn: &Connection, cmap: Colormap) -> Result { + pub fn new_with(conn: &RawConnection, cmap: Colormap) -> Result { // TODO: Move these colours out to config.rs let (border_normal, border_focused) = ( conn.wait_for_reply(conn.send_request(&AllocColor { @@ -47,4 +47,14 @@ impl Colours { pub const fn border_focused(&self) -> u32 { self.border_focused } + + /// Free the associated resources to avoid leaking them. + /// This object must not be used again after this method is called, as all resources will be invalid. + pub unsafe fn free(&self, conn: &RawConnection) { + conn.send_request(&FreeColors { + cmap: self.cmap, + plane_mask: 0, + pixels: &[self.border_normal, self.border_focused], + }); + } } -- cgit v1.2.3