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/cursors.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'src/conn_info/cursors.rs') diff --git a/src/conn_info/cursors.rs b/src/conn_info/cursors.rs index 119de5c..bb547e4 100644 --- a/src/conn_info/cursors.rs +++ b/src/conn_info/cursors.rs @@ -1,7 +1,7 @@ use crate::error::Result; use xcb::{ - x::{CreateGlyphCursor, Cursor, Font, OpenFont}, - Connection, + x::{CloseFont, CreateGlyphCursor, Cursor, Font, FreeCursor, OpenFont}, + Connection as RawConnection, }; // https://tronche.com/gui/x/xlib/appendix/b/ @@ -18,7 +18,7 @@ pub struct Cursors { impl Cursors { /// Load default cursors using the given connection. - pub fn new_with(conn: &Connection) -> Result { + pub fn new_with(conn: &RawConnection) -> Result { // Open cursor font let font = conn.generate_id(); conn.check_request(conn.send_request_checked(&OpenFont { @@ -33,7 +33,7 @@ impl Cursors { } /// Load the cursor with the given id from `font` - fn load_cursor(conn: &Connection, font: Font, id: u16) -> Result { + fn load_cursor(conn: &RawConnection, font: Font, id: u16) -> Result { let cid = conn.generate_id(); // https://github.com/mirror/libX11/blob/ff8706a5eae25b8bafce300527079f68a201d27f/src/Cursor.c#L34 conn.check_request(conn.send_request_checked(&CreateGlyphCursor { @@ -56,4 +56,13 @@ impl Cursors { pub const fn normal(&self) -> Cursor { self.normal } + + /// 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(&FreeCursor { + cursor: self.normal, + }); + conn.send_request(&CloseFont { font: self.font }); + } } -- cgit v1.2.3