From d5ab024521168449159f6694ff8a23f9d2198e09 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 15 Apr 2022 07:52:08 -0700 Subject: [PATCH] fix memory leaks --- src/Grid.zig | 9 +++++++-- src/Window.zig | 6 ++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Grid.zig b/src/Grid.zig index 88826a7ea..5441a5110 100644 --- a/src/Grid.zig +++ b/src/Grid.zig @@ -51,9 +51,9 @@ pub fn init(alloc: Allocator) !Grid { // Initialize our font atlas. We will initially populate the // font atlas with all the visible ASCII characters since they are common. var atlas = try Atlas.init(alloc, 512); - errdefer atlas.deinit(alloc); + defer atlas.deinit(alloc); var font = try FontAtlas.init(atlas); - errdefer font.deinit(alloc); + defer font.deinit(alloc); try font.loadFaceFromMemory(face_ttf, 30); // Load all visible ASCII characters and build our cell width based on @@ -104,6 +104,11 @@ pub fn init(alloc: Allocator) !Grid { }; } +pub fn deinit(self: *Grid) void { + self.program.destroy(); + self.* = undefined; +} + /// Set the screen size for rendering. This will update the projection /// used for the shader so that the scaling of the grid is correct. pub fn setScreenSize(self: *Grid, dim: ScreenDim) !void { diff --git a/src/Window.zig b/src/Window.zig index d39643f7c..9df46511d 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -77,6 +77,7 @@ pub fn create(alloc: Allocator) !*Window { } pub fn destroy(self: *Window, alloc: Allocator) void { + self.grid.deinit(); alloc.destroy(self); } @@ -102,8 +103,9 @@ fn sizeCallback(window: glfw.Window, width: i32, height: i32) void { win.grid.setScreenSize(.{ .width = width, .height = height, - }) catch unreachable; + }) catch |err| log.err("error updating grid screen size err={}", .{err}); // Update our viewport for this context to be the entire window - try gl.viewport(0, 0, width, height); + gl.viewport(0, 0, width, height) catch |err| + log.err("error updating OpenGL viewport err={}", .{err}); }