Fixed some additional memory leaks.

This commit is contained in:
Alexandros Naskos 2020-05-07 13:56:08 +03:00
parent 3fa5ef4b96
commit e9d9c57ff4

View File

@ -82,6 +82,7 @@ pub fn publishDiagnostics(document: types.TextDocument) !void {
defer tree.deinit(); defer tree.deinit();
var diagnostics = std.ArrayList(types.Diagnostic).init(allocator); var diagnostics = std.ArrayList(types.Diagnostic).init(allocator);
defer diagnostics.deinit();
if (tree.errors.len > 0) { if (tree.errors.len > 0) {
var index: usize = 0; var index: usize = 0;
@ -107,6 +108,9 @@ pub fn publishDiagnostics(document: types.TextDocument) !void {
.severity = types.DiagnosticSeverity.Error, .severity = types.DiagnosticSeverity.Error,
.code = @tagName(err.*), .code = @tagName(err.*),
.source = "zls", .source = "zls",
// TODO: This is wrong, reference to mem_buffer escapes
// We should probably dupe this (as well as the messages from the other branch)
// And free them in the defer along with the whole array list memory.
.message = fbs.getWritten(), .message = fbs.getWritten(),
// .relatedInformation = undefined // .relatedInformation = undefined
}); });
@ -150,7 +154,7 @@ pub fn publishDiagnostics(document: types.TextDocument) !void {
.params = types.NotificationParams{ .params = types.NotificationParams{
.PublishDiagnosticsParams = types.PublishDiagnosticsParams{ .PublishDiagnosticsParams = types.PublishDiagnosticsParams{
.uri = document.uri, .uri = document.uri,
.diagnostics = diagnostics.toOwnedSlice() .diagnostics = diagnostics.items,
} }
} }
}); });
@ -158,12 +162,28 @@ pub fn publishDiagnostics(document: types.TextDocument) !void {
pub fn completeGlobal(id: i64, document: types.TextDocument) !void { pub fn completeGlobal(id: i64, document: types.TextDocument) !void {
const tree = try std.zig.parse(allocator, document.text); const tree = try std.zig.parse(allocator, document.text);
// defer tree.deinit(); defer tree.deinit();
if (tree.errors.len > 0) return try respondGeneric(id, no_completions_response); if (tree.errors.len > 0) return try respondGeneric(id, no_completions_response);
var completions = std.ArrayList(types.CompletionItem).init(allocator); var completions = std.ArrayList(types.CompletionItem).init(allocator);
defer {
// Free the completion data.
// @TODO: Check what the tree functions (e.g. tokenSlice) return.
// We may need to dupe them to free the tree
// TODO: Maybe split off into a function or add a method to MarkupContent/CompletionItem?
for (completions.items) |comp| {
if (comp.documentation) |doc| {
if (doc.value.len > 0) {
allocator.free(doc.value);
}
}
}
completions.deinit();
}
// try log("{}", .{&tree.root_node.decls}); // try log("{}", .{&tree.root_node.decls});
var decls = tree.root_node.decls.iterator(0); var decls = tree.root_node.decls.iterator(0);
while (decls.next()) |decl_ptr| { while (decls.next()) |decl_ptr| {
@ -208,7 +228,7 @@ pub fn completeGlobal(id: i64, document: types.TextDocument) !void {
.result = types.ResponseParams{ .result = types.ResponseParams{
.CompletionList = types.CompletionList{ .CompletionList = types.CompletionList{
.isIncomplete = false, .isIncomplete = false,
.items = completions.toOwnedSlice() .items = completions.items,
} }
} }
}); });