bolts: Fix shmem leak when Drop-ing CommonUnixShMem (#1484)

This commit is contained in:
Alexander Qi 2023-08-30 00:10:59 +08:00 committed by GitHub
parent c91fc9a521
commit 51e4d814fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -623,7 +623,7 @@ pub mod unix_shmem {
use libc::{ use libc::{
c_int, c_long, c_uchar, c_uint, c_ulong, c_ushort, close, ftruncate, mmap, munmap, c_int, c_long, c_uchar, c_uint, c_ulong, c_ushort, close, ftruncate, mmap, munmap,
perror, shm_open, shm_unlink, shmat, shmctl, shmget, perror, shm_open, shm_unlink, shmat, shmctl, shmdt, shmget,
}; };
use crate::{ use crate::{
@ -971,13 +971,15 @@ pub mod unix_shmem {
} }
} }
/// [`Drop`] implementation for [`UnixShMem`], which cleans up the mapping. /// [`Drop`] implementation for [`UnixShMem`], which detaches the memory and cleans up the mapping.
#[cfg(unix)] #[cfg(unix)]
impl Drop for CommonUnixShMem { impl Drop for CommonUnixShMem {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
let id_int: i32 = self.id.into(); let id_int: i32 = self.id.into();
shmctl(id_int, libc::IPC_RMID, ptr::null_mut()); shmctl(id_int, libc::IPC_RMID, ptr::null_mut());
shmdt(self.map as *mut _);
} }
} }
} }