diff --git a/src/App.zig b/src/App.zig index fceab0275..02089ae5b 100644 --- a/src/App.zig +++ b/src/App.zig @@ -76,6 +76,15 @@ first: bool = true, pub const CreateError = Allocator.Error || font.SharedGridSet.InitError; +/// Create a new app instance. This returns a stable pointer to the app +/// instance which is required for callbacks. +pub fn create(alloc: Allocator) CreateError!*App { + var app = try alloc.create(App); + errdefer alloc.destroy(app); + try app.init(alloc); + return app; +} + /// Initialize the main app instance. This creates the main window, sets /// up the renderer state, compiles the shaders, etc. This is the primary /// "startup" logic. @@ -111,6 +120,14 @@ pub fn deinit(self: *App) void { self.font_grid_set.deinit(); } +pub fn destroy(self: *App) void { + // Deinitialize the app + self.deinit(); + + // Free the app memory + self.alloc.destroy(self); +} + /// Tick ticks the app loop. This will drain our mailbox and process those /// events. This should be called by the application runtime on every loop /// tick. diff --git a/src/apprt/embedded.zig b/src/apprt/embedded.zig index 307efd26a..dec1e4135 100644 --- a/src/apprt/embedded.zig +++ b/src/apprt/embedded.zig @@ -1317,12 +1317,8 @@ pub const CAPI = struct { opts: *const apprt.runtime.App.Options, config: *const Config, ) !*App { - var core_app = try global.alloc.create(CoreApp); - try core_app.init(global.alloc); - errdefer { - core_app.deinit(); - global.alloc.destroy(core_app); - } + const core_app = try CoreApp.create(global.alloc); + errdefer core_app.destroy(); // Create our runtime app var app = try global.alloc.create(App); @@ -1350,8 +1346,7 @@ pub const CAPI = struct { const core_app = v.core_app; v.terminate(); global.alloc.destroy(v); - core_app.deinit(); - global.alloc.destroy(core_app); + core_app.destroy(); } /// Update the focused state of the app. diff --git a/src/main_ghostty.zig b/src/main_ghostty.zig index ca63c8195..b75d8397c 100644 --- a/src/main_ghostty.zig +++ b/src/main_ghostty.zig @@ -98,9 +98,8 @@ pub fn main() !MainReturn { } // Create our app state - var app: App = undefined; - try app.init(alloc); - defer app.deinit(); + const app: *App = try App.create(alloc); + defer app.destroy(); // Create our runtime app var app_runtime: apprt.App = undefined;