-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
windows: fix memory leak
The buffer must finally be released, so use a RAAI allocation wrapper to handle this (credits to [email protected] for providing a generic implementation). Also handle errors in a uniform way and use the winapi constants.
Nils Jeisecke
committed
Apr 18, 2024
1 parent
65c47d9
commit 7a4e96d
Showing
4 changed files
with
137 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use std::ffi::c_void; | ||
|
||
pub(crate) struct FFIAlloc<T> { | ||
ptr: *mut T, | ||
} | ||
|
||
impl<T> FFIAlloc<T> { | ||
pub fn alloc(buffer_size: usize) -> Option<Self> { | ||
let ptr = unsafe { libc::malloc(buffer_size) as *mut T }; | ||
if ptr.is_null() { | ||
None | ||
} else { | ||
Some(Self { ptr }) | ||
} | ||
} | ||
|
||
pub const fn as_ptr(&self) -> *const T { | ||
self.ptr | ||
} | ||
|
||
pub const fn as_mut_ptr(&self) -> *mut T { | ||
self.ptr | ||
} | ||
} | ||
|
||
impl<T> Drop for FFIAlloc<T> { | ||
fn drop(&mut self) { | ||
unsafe { libc::free(self.ptr as *mut c_void) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters