Skip to content

Commit

Permalink
Fix memory management in Index.getInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
lalinsky committed Dec 5, 2024
1 parent ada9e0f commit a485ca0
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Index.zig
Original file line number Diff line number Diff line change
Expand Up @@ -513,15 +513,26 @@ pub fn getInfo(self: *Self, allocator: std.mem.Allocator) !IndexInfo {
defer self.releaseSegments(&snapshot); // FIXME this possibly deletes orphaned segments, do it in a separate thread

var attributes: std.StringHashMapUnmanaged(u64) = .{};
errdefer attributes.deinit(allocator);
errdefer {
var iter = attributes.iterator();
while (iter.next()) |e| {
allocator.free(e.key_ptr.*);
}
attributes.deinit(allocator);
}

var version: u64 = 0;
inline for (segment_lists) |n| {
const segments = @field(snapshot, n);
for (segments.value.nodes.items) |node| {
var iter = node.value.attributes.iterator();
while (iter.next()) |entry| {
try attributes.put(allocator, entry.key_ptr.*, entry.value_ptr.*);
const result = try attributes.getOrPut(allocator, entry.key_ptr.*);
if (!result.found_existing) {
errdefer attributes.removeByPtr(entry.key_ptr);
result.key_ptr.* = try allocator.dupe(u8, entry.key_ptr.*);
}
result.value_ptr.* = entry.value_ptr.*;
}
std.debug.assert(node.value.info.version > version);
version = node.value.info.version;
Expand Down

0 comments on commit a485ca0

Please sign in to comment.