From 6d54ca0c1754e2a1896708294378d9d8065bd490 Mon Sep 17 00:00:00 2001 From: zilcH40 Date: Wed, 8 Feb 2023 19:58:32 +0800 Subject: [PATCH] fix: typical memory leak because of into_raw --- src/rime.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/rime.rs b/src/rime.rs index 3a82a93..8062ce5 100644 --- a/src/rime.rs +++ b/src/rime.rs @@ -96,6 +96,15 @@ impl Rime { librime::RimeSyncUserData(); librime::RimeJoinMaintenanceThread(); } + // retake pointer + let _ = CString::from_raw(traits.shared_data_dir as *mut i8); + let _ = CString::from_raw(traits.user_data_dir as *mut i8); + #[cfg(not(feature = "no_log_dir"))] + let _ = CString::from_raw(traits.log_dir as *mut i8); + let _ = CString::from_raw(traits.distribution_name as *mut i8); + let _ = CString::from_raw(traits.distribution_code_name as *mut i8); + let _ = CString::from_raw(traits.distribution_version as *mut i8); + let _ = CString::from_raw(traits.app_name as *mut i8); } Ok(Rime) } @@ -249,8 +258,9 @@ impl Rime { pub fn new_session_with_keys(&self, keys: &[u8]) -> Result { let session_id = unsafe { librime::RimeCreateSession() }; unsafe { - let ck = CString::new(keys)?; - librime::RimeSimulateKeySequence(session_id, ck.into_raw()); + let ck = CString::new(keys)?.into_raw(); + librime::RimeSimulateKeySequence(session_id, ck); + let _ = CString::from_raw(ck); } Ok(session_id) }