Skip to content

Instantly share code, notes, and snippets.

@WaffleLapkin
Last active June 13, 2019 14:10
Show Gist options
  • Save WaffleLapkin/2477669a20e6cc1de157cbb59b264ef1 to your computer and use it in GitHub Desktop.
Save WaffleLapkin/2477669a20e6cc1de157cbb59b264ef1 to your computer and use it in GitHub Desktop.
example of `ggez::graphics::Font::new` leaking memory
//! Basic hello world example.
extern crate cgmath;
extern crate ggez;
use ggez::event;
use ggez::graphics;
use ggez::{Context, GameResult};
use std::env;
use std::path;
use ggez::graphics::DrawParam;
struct MainState {
font: graphics::Font
}
impl MainState {
fn new(ctx: &mut Context) -> GameResult<MainState> {
let font = graphics::Font::new(ctx, "/DejaVuSerif.ttf")?;
let s = MainState { font };
Ok(s)
}
}
impl event::EventHandler for MainState {
fn update(&mut self, _ctx: &mut Context) -> GameResult {
Ok(())
}
fn draw(&mut self, ctx: &mut Context) -> GameResult {
graphics::clear(ctx, [0.1, 0.2, 0.3, 1.0].into());
let text = graphics::Text::new(
(
"Sample text",
// If the line belong will be replaced with `self.font,` the memory leaks will be gone
graphics::Font::new(ctx, "/DejaVuSerif.ttf")?,
48.0
)
);
graphics::draw(ctx, &text, DrawParam::default().dest(cgmath::Point2::new(10.0, 10.0)))?;
graphics::present(ctx)?;
Ok(())
}
}
pub fn main() -> GameResult {
let resource_dir = if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
let mut path = path::PathBuf::from(manifest_dir);
path.push("resources");
path
} else {
path::PathBuf::from("./resources")
};
let cb = ggez::ContextBuilder::new("helloworld", "ggez").add_resource_path(resource_dir);
let (ctx, event_loop) = &mut cb.build()?;
let state = &mut MainState::new(ctx)?;
event::run(ctx, event_loop, state)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment