From a9dd430cc7ef1ae15b0920f7b4c5b3f5611aa88d Mon Sep 17 00:00:00 2001 From: Marto Date: Wed, 8 Apr 2026 14:20:17 +0200 Subject: [PATCH] Fixed memory leaks --- src/infrastructure/node_manager.zig | 3 ++- src/infrastructure/road_manager.zig | 1 + src/simulator.zig | 23 ++++++++--------------- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/infrastructure/node_manager.zig b/src/infrastructure/node_manager.zig index b9769ca..0e08b16 100644 --- a/src/infrastructure/node_manager.zig +++ b/src/infrastructure/node_manager.zig @@ -93,7 +93,7 @@ pub const NodeManager = struct { } /// Removes the node from the list with all appropriate checks - pub fn remove(self: *NodeManager, node_to_remove: *Node) !void { + pub fn remove(self: *NodeManager, allocator: std.mem.Allocator, node_to_remove: *Node) !void { // In case the node has references to the existing roads we can not remove it // This also means we don't have to deinit it, since it has no elements if (node_to_remove.roads.items.len > 0) return; @@ -101,6 +101,7 @@ pub const NodeManager = struct { for (self.nodes.items, 0..) |*node, i| { if (node.id != node_to_remove.id) continue; + node.deinit(allocator); _ = self.nodes.swapRemove(i); return; } diff --git a/src/infrastructure/road_manager.zig b/src/infrastructure/road_manager.zig index e3c8cd2..376c37f 100644 --- a/src/infrastructure/road_manager.zig +++ b/src/infrastructure/road_manager.zig @@ -62,6 +62,7 @@ pub const RoadManager = struct { /// Removes the road which reference is passed in; returns error if the road is invalid pub fn remove(self: *RoadManager, road_to_remove: *Road) !void { + // todo fix leak try road_to_remove.unreferenceNodes(); for (self.roads.items, 0..) |*road, i| { diff --git a/src/simulator.zig b/src/simulator.zig index e19a91a..c738bd5 100644 --- a/src/simulator.zig +++ b/src/simulator.zig @@ -85,10 +85,10 @@ pub const Simulator = struct { std.debug.panic("Failed to remove the road: {}\n", .{err}); }; - self.node_man.remove(nodes[0]) catch |err| { + self.node_man.remove(self.allocator, nodes[0]) catch |err| { std.debug.panic("Failed to remove the first node of the road to be deleted: {}\n", .{err}); }; - self.node_man.remove(nodes[1]) catch |err| { + self.node_man.remove(self.allocator, nodes[1]) catch |err| { std.debug.panic("Failed to remove the second node of the road to be deleted: {}\n", .{err}); }; } @@ -115,7 +115,7 @@ pub const Simulator = struct { fn cancelBuildingRoad(self: *Simulator) void { if (self.node_man.temp_node == null) return; - self.node_man.remove(self.node_man.temp_node.?) catch |err| { + self.node_man.remove(self.allocator, self.node_man.temp_node.?) catch |err| { std.debug.panic("Node doesn't exist: {}\n", .{err}); }; self.node_man.temp_node = null; @@ -163,7 +163,7 @@ pub const Simulator = struct { std.debug.panic("Failed to capture highlighted entity info: {}\n", .{err}); }; - rl.drawText(entityInfo, c.WIDTH - (c.TEXT_SIZE / 2) * @as(i32, @intCast(entityInfo.len)), c.TEXT_SIZE, c.TEXT_SIZE, .black); + rl.drawText(entityInfo, c.WIDTH - 30 * @as(i32, @intCast(entityInfo.len)), c.TEXT_SIZE / 2, c.TEXT_SIZE, .black); } /// Gets list of pointers of all roads that 'collide' with the road bounded by the nodes we pass into it @@ -188,13 +188,6 @@ pub const Simulator = struct { } } - // TODO verify pointers - // TODO display road/node id - - fn verifyPointerIntegrity() void { - - } - pub fn update(self: *Simulator) void { const pos = rl.getMousePosition(); @@ -203,15 +196,15 @@ pub const Simulator = struct { } /// Get ID info of the highlighted info and returns it - fn getHighlightedEntityInfo(self: *const Simulator) ![:0]u8 { - var buf: [10 * 1024]u8 = undefined; + fn getHighlightedEntityInfo(self: *const Simulator) ![:0]const u8 { + var buf: [1024]u8 = undefined; if (self.node_man.highlighted_node) |node| - return std.fmt.bufPrintZ(&buf, "Node ID: {d}\nRoad refs: {d}", .{node.id, node.roads.items.len}); + return std.fmt.bufPrintZ(&buf, "Node ID: {d}", .{node.id}); if (self.road_man.highlighted_road) |road| return std.fmt.bufPrintZ(&buf, "Road ID: {d}", .{road.id}); - return std.fmt.bufPrintZ(&buf, "", .{}); + return ""; } }; \ No newline at end of file