commit aa6b89c4a2fc1b1bb51d7c182486abe07b22e1c9
parent e2bc0ba7e6a2b82a3e3f498cb701e5ceca0f4050
Author: Christian Ermann <christianermann@gmail.com>
Date: Sat, 20 Jul 2024 11:21:32 -0400
Fix memory leaks in vertex cache optimization
Diffstat:
1 file changed, 12 insertions(+), 0 deletions(-)
diff --git a/src/forsyth_optimize.zig b/src/forsyth_optimize.zig
@@ -79,6 +79,12 @@ fn LRUCache(comptime T: type) type {
};
}
+ pub fn deinit(self: *Self) void {
+ while (self.list.pop()) |node| {
+ self.allocator.destroy(node);
+ }
+ }
+
fn find(cache: *const Self, value: T) ?*List.Node {
var it = cache.list.first;
while (it) |node| : (it = node.next) {
@@ -119,6 +125,8 @@ pub fn optimize(
) !void {
// Initialize vertex data structure
const vertices = try allocator.alloc(Vertex, mesh.positions.len);
+ defer allocator.free(vertices);
+
@memset(vertices, Vertex{});
for (0..mesh.indices.len / 3) |i| {
const mesh_offset = i * 3;
@@ -155,6 +163,8 @@ pub fn optimize(
// Initialize triangle data structure
const triangles = try allocator.alloc(Triangle, mesh.indices.len / 3);
+ defer allocator.free(triangles);
+
@memset(triangles, Triangle{});
for (triangles, 0..) |*tri, i| {
const mesh_offset = i * 3;
@@ -172,6 +182,8 @@ pub fn optimize(
// Run algorithm
var cache = LRUCache(u32).init(allocator, max_cache_idx);
+ defer cache.deinit();
+
for (0..triangles.len) |i| {
var tri = bestTriangleFromCache(vertices, triangles, &cache);
if (tri == null) {