From 53823719549e1fc6367b2469951c67979d592edb Mon Sep 17 00:00:00 2001 From: Madeorsk Date: Tue, 26 Nov 2024 10:24:50 +0100 Subject: [PATCH] Fix memory leaks when an error is returned while reading results. --- src/result.zig | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/result.zig b/src/result.zig index 2e65e96..654ff09 100644 --- a/src/result.zig +++ b/src/result.zig @@ -131,6 +131,7 @@ pub fn ResultMapper(comptime Model: type, comptime TableShape: type, comptime Me // Create an arena for mapper data. var mapperArena = std.heap.ArenaAllocator.init(allocator); + errdefer mapperArena.deinit(); // Initialize query result reader. const reader = try queryReader.init(mapperArena.allocator()); @@ -166,6 +167,13 @@ pub fn ResultMapper(comptime Model: type, comptime TableShape: type, comptime Me try models.append(model); } + errdefer { + // Destroy models when something bad happen. + for (models.items) |model| { + allocator.destroy(model); + } + } + if (relations) |relationsToLoad| { inline for (relationsToLoad) |relation| { // Build query for the relation to get.