From 9e4560043a58199ff6d4fd2702c85b7a3a0aa81e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 22 Feb 2023 14:58:20 -0800 Subject: [PATCH] fix crashes on close --- src/App.zig | 4 +--- src/apprt/glfw.zig | 11 ++++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/App.zig b/src/App.zig index 6819d5059..c28faebf7 100644 --- a/src/App.zig +++ b/src/App.zig @@ -109,9 +109,7 @@ pub fn tick(self: *App, rt_app: *apprt.runtime.App) !bool { while (i < self.surfaces.items.len) { const surface = self.surfaces.items[i]; if (surface.shouldClose()) { - surface.deinit(); - _ = self.surfaces.swapRemove(i); - self.surface_pool.destroy(surface); + rt_app.closeSurface(surface); continue; } diff --git a/src/apprt/glfw.zig b/src/apprt/glfw.zig index beb560622..0aa2b5f44 100644 --- a/src/apprt/glfw.zig +++ b/src/apprt/glfw.zig @@ -86,6 +86,12 @@ pub const App = struct { errdefer surface.deinit(); } + /// Close the given surface. + pub fn closeSurface(self: *App, surface: *Surface) void { + surface.deinit(); + self.app.surface_pool.destroy(surface); + } + fn glfwErrorCallback(code: glfw.ErrorCode, desc: [:0]const u8) void { std.log.warn("glfw error={} message={s}", .{ code, desc }); @@ -237,7 +243,10 @@ pub const Surface = struct { } pub fn deinit(self: *Surface) void { - // First clean up our core surface so that all the rendering and IO stop. + // Remove ourselves from the list of known surfaces in the app. + self.core_surface.app.deleteSurface(self); + + // Clean up our core surface so that all the rendering and IO stop. self.core_surface.deinit(); var tabgroup_opt: if (App.Darwin.enabled) ?objc.Object else void = undefined;