Fixed memory leaks
This commit is contained in:
@@ -93,7 +93,7 @@ pub const NodeManager = struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Removes the node from the list with all appropriate checks
|
/// 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
|
// 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
|
// This also means we don't have to deinit it, since it has no elements
|
||||||
if (node_to_remove.roads.items.len > 0) return;
|
if (node_to_remove.roads.items.len > 0) return;
|
||||||
@@ -101,6 +101,7 @@ pub const NodeManager = struct {
|
|||||||
for (self.nodes.items, 0..) |*node, i| {
|
for (self.nodes.items, 0..) |*node, i| {
|
||||||
if (node.id != node_to_remove.id) continue;
|
if (node.id != node_to_remove.id) continue;
|
||||||
|
|
||||||
|
node.deinit(allocator);
|
||||||
_ = self.nodes.swapRemove(i);
|
_ = self.nodes.swapRemove(i);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,6 +62,7 @@ pub const RoadManager = struct {
|
|||||||
|
|
||||||
/// Removes the road which reference is passed in; returns error if the road is invalid
|
/// 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 {
|
pub fn remove(self: *RoadManager, road_to_remove: *Road) !void {
|
||||||
|
// todo fix leak
|
||||||
try road_to_remove.unreferenceNodes();
|
try road_to_remove.unreferenceNodes();
|
||||||
|
|
||||||
for (self.roads.items, 0..) |*road, i| {
|
for (self.roads.items, 0..) |*road, i| {
|
||||||
|
|||||||
@@ -85,10 +85,10 @@ pub const Simulator = struct {
|
|||||||
std.debug.panic("Failed to remove the road: {}\n", .{err});
|
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});
|
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});
|
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 {
|
fn cancelBuildingRoad(self: *Simulator) void {
|
||||||
if (self.node_man.temp_node == null) return;
|
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});
|
std.debug.panic("Node doesn't exist: {}\n", .{err});
|
||||||
};
|
};
|
||||||
self.node_man.temp_node = null;
|
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});
|
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
|
/// 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 {
|
pub fn update(self: *Simulator) void {
|
||||||
const pos = rl.getMousePosition();
|
const pos = rl.getMousePosition();
|
||||||
|
|
||||||
@@ -203,15 +196,15 @@ pub const Simulator = struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get ID info of the highlighted info and returns it
|
/// Get ID info of the highlighted info and returns it
|
||||||
fn getHighlightedEntityInfo(self: *const Simulator) ![:0]u8 {
|
fn getHighlightedEntityInfo(self: *const Simulator) ![:0]const u8 {
|
||||||
var buf: [10 * 1024]u8 = undefined;
|
var buf: [1024]u8 = undefined;
|
||||||
|
|
||||||
if (self.node_man.highlighted_node) |node|
|
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|
|
if (self.road_man.highlighted_road) |road|
|
||||||
return std.fmt.bufPrintZ(&buf, "Road ID: {d}", .{road.id});
|
return std.fmt.bufPrintZ(&buf, "Road ID: {d}", .{road.id});
|
||||||
|
|
||||||
return std.fmt.bufPrintZ(&buf, "", .{});
|
return "";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user