Don't leak the struct_FT_MemoryRec_.

We libc::malloc() a struct_FT_MemoryRec_ and pass it to FreeType. We
need to libc::free() it as well when FreeType is done with it.
This commit is contained in:
Nicholas Nethercote 2015-05-27 21:22:11 -07:00
parent af81db5479
commit 16ba32521b
2 changed files with 15 additions and 11 deletions

View file

@ -40,6 +40,17 @@ extern fn ft_realloc(_mem: FT_Memory, _cur_size: c_long, new_size: c_long, block
#[derive(Clone)] #[derive(Clone)]
pub struct FreeTypeLibraryHandle { pub struct FreeTypeLibraryHandle {
pub ctx: FT_Library, pub ctx: FT_Library,
pub mem: FT_Memory,
}
impl Drop for FreeTypeLibraryHandle {
fn drop(&mut self) {
assert!(!self.ctx.is_null());
unsafe {
FT_Done_Library(self.ctx);
libc::free(self.mem as *mut c_void);
}
}
} }
#[derive(Clone)] #[derive(Clone)]
@ -47,20 +58,12 @@ pub struct FontContextHandle {
pub ctx: Rc<FreeTypeLibraryHandle>, pub ctx: Rc<FreeTypeLibraryHandle>,
} }
impl Drop for FreeTypeLibraryHandle {
fn drop(&mut self) {
assert!(!self.ctx.is_null());
unsafe { FT_Done_Library(self.ctx) };
}
}
impl FontContextHandle { impl FontContextHandle {
pub fn new() -> FontContextHandle { pub fn new() -> FontContextHandle {
unsafe { unsafe {
let ptr = libc::malloc(mem::size_of::<struct_FT_MemoryRec_>() as size_t); let ptr = libc::malloc(mem::size_of::<struct_FT_MemoryRec_>() as size_t);
let allocator: &mut struct_FT_MemoryRec_ = mem::transmute(ptr); let mem: &mut struct_FT_MemoryRec_ = mem::transmute(ptr);
ptr::write(allocator, struct_FT_MemoryRec_ { ptr::write(mem, struct_FT_MemoryRec_ {
user: ptr::null_mut(), user: ptr::null_mut(),
alloc: ft_alloc, alloc: ft_alloc,
free: ft_free, free: ft_free,
@ -75,7 +78,7 @@ impl FontContextHandle {
FT_Add_Default_Modules(ctx); FT_Add_Default_Modules(ctx);
FontContextHandle { FontContextHandle {
ctx: Rc::new(FreeTypeLibraryHandle { ctx: ctx }), ctx: Rc::new(FreeTypeLibraryHandle { ctx: ctx, mem: mem }),
} }
} }
} }

View file

@ -479,6 +479,7 @@ dependencies = [
"layers 0.1.0 (git+https://github.com/servo/rust-layers)", "layers 0.1.0 (git+https://github.com/servo/rust-layers)",
"libc 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
"msg 0.0.1", "msg 0.0.1",
"net 0.0.1",
"script_traits 0.0.1", "script_traits 0.0.1",
"time 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
"url 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", "url 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",