tiehuis / zig-regex

A regex implementation for the zig programming language
MIT License
196 stars 24 forks source link

"Memory Leak Detected" when compiling basic example. #10

Closed cr1901 closed 3 years ago

cr1901 commented 4 years ago

Consider the following example application taken from my Advent Of Code 2016 Repo:

const std = @import("std");
const Regex = @import("regex").Regex;

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    try stdout.print("AOC 2016 Day 4\n", .{});

    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();

    var re = try Regex.compile(&gpa.allocator, "\\w+");
    defer re.deinit();
}

When I run zig build day4 from the root of my repo, I get reports of a memory leak that I'm unsure how to diagnose:

william@xubuntu-dtrain:~/Projects/zig/aoc-2016$ zig build day4
AOC 2016 Day 4
Memory leak detected:
/home/william/.local/lib/zig/std/array_list.zig:272:65: 0x23d413 in std.array_list.ArrayListAligned(.regex.range_set.Range(u8),null).ensureCapacity (day4)
            const new_memory = try self.allocator.reallocAtLeast(self.allocatedSlice(), better_capacity);
                                                                ^
/home/william/.local/lib/zig/std/array_list.zig:288:36: 0x23d35f in std.array_list.ArrayListAligned(.regex.range_set.Range(u8),null).addOne (day4)
            try self.ensureCapacity(newlen);
                                   ^
/home/william/.local/lib/zig/std/array_list.zig:145:49: 0x23cd0a in std.array_list.ArrayListAligned(.regex.range_set.Range(u8),null).append (day4)
            const new_item_ptr = try self.addOne();
                                                ^
/home/william/Projects/zig/aoc-2016/extern/zig-regex/src/range_set.zig:65:34: 0x23c534 in .regex.range_set.RangeSet(u8).addRange (day4)
                try ranges.append(range);
                                 ^

william@xubuntu-dtrain:~/Projects/zig/aoc-2016$

I have attempted to make Regex.deinit also deallocate the ArrayList locally (by adding re.slots.deinit()), but it does not have any effect on removing memory leak.

kivikakk commented 4 years ago

I've started trying to address this (at #7), but stalled out.

tiehuis commented 3 years ago

Merged the above PR and saw these errors resolved on my current version 0.8.0-dev.1471+4e9894cfc.