Skip to content

Commit

Permalink
fix: typical memory leak because of into_raw
Browse files Browse the repository at this point in the history
  • Loading branch information
wlh320 committed Feb 8, 2023
1 parent 4ab442f commit 6d54ca0
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/rime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -249,8 +258,9 @@ impl Rime {
pub fn new_session_with_keys(&self, keys: &[u8]) -> Result<usize, NulError> {
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)
}
Expand Down

0 comments on commit 6d54ca0

Please sign in to comment.