Fix memory leaks when an error is returned while reading results.

This commit is contained in:
Madeorsk 2024-11-26 10:24:50 +01:00
parent 56d9414dfa
commit 5382371954
Signed by: Madeorsk
GPG key ID: 677E51CA765BB79F

View file

@ -131,6 +131,7 @@ pub fn ResultMapper(comptime Model: type, comptime TableShape: type, comptime Me
// Create an arena for mapper data. // Create an arena for mapper data.
var mapperArena = std.heap.ArenaAllocator.init(allocator); var mapperArena = std.heap.ArenaAllocator.init(allocator);
errdefer mapperArena.deinit();
// Initialize query result reader. // Initialize query result reader.
const reader = try queryReader.init(mapperArena.allocator()); 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); try models.append(model);
} }
errdefer {
// Destroy models when something bad happen.
for (models.items) |model| {
allocator.destroy(model);
}
}
if (relations) |relationsToLoad| { if (relations) |relationsToLoad| {
inline for (relationsToLoad) |relation| { inline for (relationsToLoad) |relation| {
// Build query for the relation to get. // Build query for the relation to get.