Memory lifetime fixes (#861)

* fix memory lifetime issues

* more memory lifetime issue fixes
This commit is contained in:
Techatrix 2022-12-29 05:59:19 +00:00 committed by GitHub
parent faee213658
commit ebe3ba1471
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -146,12 +146,11 @@ pub fn openDocument(self: *DocumentStore, uri: Uri, text: []const u8) error{OutO
return handle.*; return handle.*;
} }
const duped_text = try self.allocator.dupeZ(u8, text);
errdefer self.allocator.free(duped_text);
var handle = try self.allocator.create(Handle); var handle = try self.allocator.create(Handle);
errdefer self.allocator.destroy(handle); errdefer self.allocator.destroy(handle);
const duped_text = try self.allocator.dupeZ(u8, text);
handle.* = try self.createDocument(uri, duped_text, true); handle.* = try self.createDocument(uri, duped_text, true);
errdefer handle.deinit(self.allocator); errdefer handle.deinit(self.allocator);
@ -580,7 +579,9 @@ fn uriInImports(
return false; return false;
// consider it checked even if a failure happens // consider it checked even if a failure happens
try checked_uris.put(try self.allocator.dupe(u8, source_uri), {});
const duped_uri = try self.allocator.dupe(u8, source_uri);
checked_uris.put(duped_uri, {}) catch self.allocator.free(duped_uri);
const handle = self.getOrLoadHandle(source_uri) orelse return false; const handle = self.getOrLoadHandle(source_uri) orelse return false;
@ -638,11 +639,16 @@ fn createDocument(self: *DocumentStore, uri: Uri, text: [:0]u8, open: bool) erro
// TODO: Better logic for detecting std or subdirectories? // TODO: Better logic for detecting std or subdirectories?
const in_std = std.mem.indexOf(u8, uri, "/std/") != null; const in_std = std.mem.indexOf(u8, uri, "/std/") != null;
if (self.config.zig_exe_path != null and std.mem.endsWith(u8, uri, "/build.zig") and !in_std) { if (self.config.zig_exe_path != null and std.mem.endsWith(u8, uri, "/build.zig") and !in_std) {
errdefer |err| log.debug("Failed to load build file {s}: (error: {})", .{ uri, err }); const gop = try self.build_files.getOrPut(self.allocator, uri);
const duped_uri = try self.allocator.dupe(u8, uri); errdefer |err| {
var build_file = try self.createBuildFile(duped_uri); self.build_files.swapRemoveAt(gop.index);
errdefer build_file.deinit(self.allocator); log.debug("Failed to load build file {s}: (error: {})", .{ uri, err });
try self.build_files.putNoClobber(self.allocator, build_file.uri, build_file); }
if(!gop.found_existing) {
const duped_uri = try self.allocator.dupe(u8, uri);
gop.value_ptr.* = try self.createBuildFile(duped_uri);
gop.key_ptr.* = gop.value_ptr.uri;
}
handle.is_build_file = true; handle.is_build_file = true;
} else if (self.config.zig_exe_path != null and !std.mem.endsWith(u8, uri, "/builtin.zig") and !in_std) blk: { } else if (self.config.zig_exe_path != null and !std.mem.endsWith(u8, uri, "/builtin.zig") and !in_std) blk: {
log.debug("Going to walk down the tree towards: {s}", .{uri}); log.debug("Going to walk down the tree towards: {s}", .{uri});