Fix memory leaks

Closes #262.
This commit is contained in:
Jonathan Hähne 2021-03-30 12:33:31 +02:00
parent 2bf5a7cfc9
commit b7cbb10610
2 changed files with 12 additions and 25 deletions

View File

@ -390,8 +390,8 @@ fn refreshDocument(self: *DocumentStore, handle: *Handle, zig_lib_path: ?[]const
} }
log.debug("Import removed: {s}", .{old}); log.debug("Import removed: {s}", .{old});
self.decrementCount(old); self.decrementCount(old);
self.allocator.free(old);
} }
self.allocator.free(old);
} }
} }

View File

@ -1672,7 +1672,7 @@ fn processJsonRpc(arena: *std.heap.ArenaAllocator, parser: *std.json.Parser, jso
logger.debug("Method without return value not implemented: {s}", .{method}); logger.debug("Method without return value not implemented: {s}", .{method});
} }
var gpa_state = std.heap.GeneralPurposeAllocator(.{}){}; var gpa_state = std.heap.GeneralPurposeAllocator(.{ .stack_trace_frames = 8 }){};
pub fn main() anyerror!void { pub fn main() anyerror!void {
defer _ = gpa_state.deinit(); defer _ = gpa_state.deinit();
allocator = &gpa_state.allocator; allocator = &gpa_state.allocator;
@ -1703,15 +1703,7 @@ pub fn main() anyerror!void {
const config_parse_options = std.json.ParseOptions{ .allocator = allocator }; const config_parse_options = std.json.ParseOptions{ .allocator = allocator };
var config = Config{}; var config = Config{};
var config_had_null_zig_path = config.zig_exe_path == null; var config_had_null_zig_path = config.zig_exe_path == null;
defer { defer std.json.parseFree(Config, config, config_parse_options);
if (config_had_null_zig_path) {
if (config.zig_exe_path) |exe_path| {
allocator.free(exe_path);
config.zig_exe_path = null;
}
}
std.json.parseFree(Config, config, config_parse_options);
}
config_read: { config_read: {
const res = try known_folders.getPath(allocator, .local_configuration); const res = try known_folders.getPath(allocator, .local_configuration);
@ -1736,17 +1728,12 @@ pub fn main() anyerror!void {
find_zig: { find_zig: {
if (config.zig_exe_path) |exe_path| { if (config.zig_exe_path) |exe_path| {
if (std.fs.path.isAbsolute(exe_path)) { if (std.fs.path.isAbsolute(exe_path)) not_valid: {
zig_exe_path = try std.mem.dupe(allocator, u8, exe_path); std.fs.cwd().access(exe_path, .{}) catch break :not_valid;
// make sure the path still exists
if (blk: {
std.fs.cwd().access(zig_exe_path.?, .{}) catch break :blk false;
break :blk true;
})
break :find_zig; break :find_zig;
} }
logger.debug("zig path `{s}` is not absolute, will look in path", .{exe_path}); logger.debug("zig path `{s}` is not absolute, will look in path", .{exe_path});
allocator.free(exe_path);
} }
const env_path = std.process.getEnvVarOwned(allocator, "PATH") catch |err| switch (err) { const env_path = std.process.getEnvVarOwned(allocator, "PATH") catch |err| switch (err) {
@ -1758,7 +1745,7 @@ pub fn main() anyerror!void {
}; };
defer allocator.free(env_path); defer allocator.free(env_path);
const exe_extension = @as(std.zig.CrossTarget, .{}).exeFileExt(); const exe_extension = std.Target.current.exeFileExt();
const zig_exe = try std.fmt.allocPrint(allocator, "zig{s}", .{exe_extension}); const zig_exe = try std.fmt.allocPrint(allocator, "zig{s}", .{exe_extension});
defer allocator.free(zig_exe); defer allocator.free(zig_exe);
@ -1771,15 +1758,15 @@ pub fn main() anyerror!void {
defer allocator.free(full_path); defer allocator.free(full_path);
var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
zig_exe_path = try std.mem.dupe(allocator, u8, std.os.realpath(full_path, &buf) catch continue); config.zig_exe_path = try std.mem.dupe(allocator, u8, std.os.realpath(full_path, &buf) catch continue);
logger.info("Found zig in PATH: {s}", .{zig_exe_path}); logger.info("Found zig in PATH: {s}", .{config.zig_exe_path});
break :find_zig; break :find_zig;
} }
} }
if (zig_exe_path) |exe_path| { if (config.zig_exe_path) |exe_path| {
config.zig_exe_path = exe_path;
logger.info("Using zig executable {s}", .{exe_path}); logger.info("Using zig executable {s}", .{exe_path});
if (config.zig_lib_path == null) find_lib_path: { if (config.zig_lib_path == null) find_lib_path: {
// Use `zig env` to find the lib path // Use `zig env` to find the lib path
const zig_env_result = try std.ChildProcess.exec(.{ const zig_env_result = try std.ChildProcess.exec(.{