fix memory leaks

This commit is contained in:
Mitchell Hashimoto
2022-04-15 07:52:08 -07:00
parent bb902cf4e3
commit d5ab024521
2 changed files with 11 additions and 4 deletions

View File

@ -51,9 +51,9 @@ pub fn init(alloc: Allocator) !Grid {
// Initialize our font atlas. We will initially populate the // Initialize our font atlas. We will initially populate the
// font atlas with all the visible ASCII characters since they are common. // font atlas with all the visible ASCII characters since they are common.
var atlas = try Atlas.init(alloc, 512); var atlas = try Atlas.init(alloc, 512);
errdefer atlas.deinit(alloc); defer atlas.deinit(alloc);
var font = try FontAtlas.init(atlas); var font = try FontAtlas.init(atlas);
errdefer font.deinit(alloc); defer font.deinit(alloc);
try font.loadFaceFromMemory(face_ttf, 30); try font.loadFaceFromMemory(face_ttf, 30);
// Load all visible ASCII characters and build our cell width based on // 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 /// Set the screen size for rendering. This will update the projection
/// used for the shader so that the scaling of the grid is correct. /// used for the shader so that the scaling of the grid is correct.
pub fn setScreenSize(self: *Grid, dim: ScreenDim) !void { pub fn setScreenSize(self: *Grid, dim: ScreenDim) !void {

View File

@ -77,6 +77,7 @@ pub fn create(alloc: Allocator) !*Window {
} }
pub fn destroy(self: *Window, alloc: Allocator) void { pub fn destroy(self: *Window, alloc: Allocator) void {
self.grid.deinit();
alloc.destroy(self); alloc.destroy(self);
} }
@ -102,8 +103,9 @@ fn sizeCallback(window: glfw.Window, width: i32, height: i32) void {
win.grid.setScreenSize(.{ win.grid.setScreenSize(.{
.width = width, .width = width,
.height = height, .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 // 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});
} }