From 16ba32521bdff295fea70b308150a50bb336b3af Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 27 May 2015 21:22:11 -0700 Subject: [PATCH 1/2] 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. --- .../gfx/platform/freetype/font_context.rs | 25 +++++++++++-------- components/servo/Cargo.lock | 1 + 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/components/gfx/platform/freetype/font_context.rs b/components/gfx/platform/freetype/font_context.rs index 313e02f022e..3fb35ab2dc9 100644 --- a/components/gfx/platform/freetype/font_context.rs +++ b/components/gfx/platform/freetype/font_context.rs @@ -40,6 +40,17 @@ extern fn ft_realloc(_mem: FT_Memory, _cur_size: c_long, new_size: c_long, block #[derive(Clone)] pub struct FreeTypeLibraryHandle { 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)] @@ -47,20 +58,12 @@ pub struct FontContextHandle { pub ctx: Rc, } -impl Drop for FreeTypeLibraryHandle { - fn drop(&mut self) { - assert!(!self.ctx.is_null()); - unsafe { FT_Done_Library(self.ctx) }; - } -} - impl FontContextHandle { pub fn new() -> FontContextHandle { unsafe { - let ptr = libc::malloc(mem::size_of::() as size_t); - let allocator: &mut struct_FT_MemoryRec_ = mem::transmute(ptr); - ptr::write(allocator, struct_FT_MemoryRec_ { + let mem: &mut struct_FT_MemoryRec_ = mem::transmute(ptr); + ptr::write(mem, struct_FT_MemoryRec_ { user: ptr::null_mut(), alloc: ft_alloc, free: ft_free, @@ -75,7 +78,7 @@ impl FontContextHandle { FT_Add_Default_Modules(ctx); FontContextHandle { - ctx: Rc::new(FreeTypeLibraryHandle { ctx: ctx }), + ctx: Rc::new(FreeTypeLibraryHandle { ctx: ctx, mem: mem }), } } } diff --git a/components/servo/Cargo.lock b/components/servo/Cargo.lock index b6a757d6513..171d9edebb4 100644 --- a/components/servo/Cargo.lock +++ b/components/servo/Cargo.lock @@ -479,6 +479,7 @@ dependencies = [ "layers 0.1.0 (git+https://github.com/servo/rust-layers)", "libc 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "msg 0.0.1", + "net 0.0.1", "script_traits 0.0.1", "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)", From e8fc40652001b11aaa62f1e4ec4c5530f970194f Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 27 May 2015 21:50:09 -0700 Subject: [PATCH 2/2] Reduce unsafe-ness in FontContextHandle::new(). Because `box + boxed::into_raw() + boxed::from_raw` is nicer than `libc::malloc() + mem::transmute() + ptr::write() + libc::free()`. --- .../gfx/platform/freetype/font_context.rs | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/components/gfx/platform/freetype/font_context.rs b/components/gfx/platform/freetype/font_context.rs index 3fb35ab2dc9..c60c6bf5e36 100644 --- a/components/gfx/platform/freetype/font_context.rs +++ b/components/gfx/platform/freetype/font_context.rs @@ -13,9 +13,7 @@ use freetype::freetype::struct_FT_MemoryRec_; use std::ptr; use std::rc::Rc; -use libc; -use libc::{c_void, c_long, size_t, malloc}; -use std::mem; +use libc::{self, c_void, c_long, size_t}; extern fn ft_alloc(_mem: FT_Memory, size: c_long) -> *mut c_void { unsafe { @@ -48,7 +46,7 @@ impl Drop for FreeTypeLibraryHandle { assert!(!self.ctx.is_null()); unsafe { FT_Done_Library(self.ctx); - libc::free(self.mem as *mut c_void); + Box::from_raw(self.mem); } } } @@ -60,19 +58,17 @@ pub struct FontContextHandle { impl FontContextHandle { pub fn new() -> FontContextHandle { + let mem = box struct_FT_MemoryRec_ { + user: ptr::null_mut(), + alloc: ft_alloc, + free: ft_free, + realloc: ft_realloc, + }; unsafe { - let ptr = libc::malloc(mem::size_of::() as size_t); - let mem: &mut struct_FT_MemoryRec_ = mem::transmute(ptr); - ptr::write(mem, struct_FT_MemoryRec_ { - user: ptr::null_mut(), - alloc: ft_alloc, - free: ft_free, - realloc: ft_realloc, - }); - let mut ctx: FT_Library = ptr::null_mut(); - let result = FT_New_Library(ptr as FT_Memory, &mut ctx); + let mem = ::std::boxed::into_raw(mem); + let result = FT_New_Library(mem, &mut ctx); if !result.succeeded() { panic!("Unable to initialize FreeType library"); } FT_Add_Default_Modules(ctx);