Skip to content

Commit

Permalink
Merge pull request #22 from Reokodoku/gpa-deinit
Browse files Browse the repository at this point in the history
Fix memory leak and deinit GPA
  • Loading branch information
Arnau478 authored May 12, 2024
2 parents ae969c0 + 743fdc0 commit 1ae8f13
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ fn display(reader: std.io.AnyReader, colors: []const TextColor, writer: std.io.A
}

pub fn main() !void {
defer if (gpa.deinit() == .leak) std.debug.print("Error: MEMORY LEAK!\n", .{});

const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);

Expand Down
16 changes: 11 additions & 5 deletions src/options.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub const DisplayOptions = struct {
}
};

fn openConfigFile(env_map: std.process.EnvMap) ?std.fs.File {
fn openConfigFile(env_map: std.process.EnvMap) ?std.meta.Tuple(&.{ std.fs.File, []const u8 }) {
const path: ?[]const u8 = switch (builtin.os.tag) {
.linux, .macos, .freebsd, .openbsd, .netbsd => if (env_map.get("XDG_CONFIG_HOME")) |xdg_config_home|
std.fs.path.join(allocator, &.{ xdg_config_home, "hevi/config.zon" }) catch null
Expand All @@ -48,7 +48,10 @@ fn openConfigFile(env_map: std.process.EnvMap) ?std.fs.File {
else => null,
};

return std.fs.openFileAbsolute(path orelse return null, .{}) catch null;
return .{ std.fs.openFileAbsolute(path orelse return null, .{}) catch {
allocator.free(path.?);
return null;
}, path orelse return null };
}

pub fn getOptions(args: argparse.ParseResult, stdout: std.fs.File) !DisplayOptions {
Expand All @@ -67,13 +70,16 @@ pub fn getOptions(args: argparse.ParseResult, stdout: std.fs.File) !DisplayOptio
};

// Config file
if (openConfigFile(envs)) |file| {
defer file.close();
if (openConfigFile(envs)) |tuple| {
defer {
tuple[0].close();
allocator.free(tuple[1]);
}

const stderr = std.io.getStdErr();
defer stderr.close();

const source = try file.readToEndAllocOptions(allocator, std.math.maxInt(usize), null, 1, 0);
const source = try tuple[0].readToEndAllocOptions(allocator, std.math.maxInt(usize), null, 1, 0);
defer allocator.free(source);

var ast = try std.zig.Ast.parse(allocator, source, .zon);
Expand Down

0 comments on commit 1ae8f13

Please sign in to comment.