Skip to content

Commit a022723

Browse files
committed
fix(state): add memory cleanup for allocated State fields
- Add deinit() method to State struct to free session_id and last_update - Call st.deinit() in main.zig before program exit - Fixes memory leak when State is loaded from JSON file The State.load() function allocates memory for session_id and last_update using allocator.dupe(), but this memory was never freed. This commit adds proper cleanup to prevent memory leaks. Signed-off-by: leocavalcante <[email protected]>
1 parent b9b040e commit a022723

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/main.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ fn runOpencoder(cfg: config.Config, allocator: std.mem.Allocator) !void {
112112
log.logErrorFmt("Failed to save final state: {}", .{err});
113113
};
114114

115+
// Clean up state
116+
st.deinit(allocator);
117+
115118
log.say("");
116119
log.say("Opencoder stopped");
117120
}

src/state.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ pub const State = struct {
5858
return State{};
5959
}
6060

61+
/// Free allocated memory for state fields
62+
pub fn deinit(self: *State, allocator: Allocator) void {
63+
if (self.session_id) |sid| {
64+
allocator.free(sid);
65+
}
66+
if (self.last_update.len > 0) {
67+
allocator.free(self.last_update);
68+
}
69+
}
70+
6171
/// Save state to JSON file
6272
pub fn save(self: *const State, path: []const u8, allocator: Allocator) !void {
6373
// Generate current timestamp

0 commit comments

Comments
 (0)