I am trying to write this program but a unexpected memory leak occurs.
const std = @import("std");
const Io = std.Io;
pub fn main(init: std.process.Init) !void {
const gpa = init.gpa;
var client = std.http.Client{
.allocator = gpa,
.io = init.io,
};
var redirect_buffer: [4096]u8 = undefined;
var response_body = try std.ArrayList(u8).initCapacity(gpa, 4096);
var response_writer = Io.Writer.fromArrayList(&response_body);
defer response_body.deinit(gpa);
defer client.deinit(); // <- Here
const response = try client.fetch(.{
.location = .{
.url = "https://jsonplaceholder.typicode.com/posts/1",
},
.method = .GET,
.redirect_buffer = &redirect_buffer,
.response_writer = &response_writer,
});
if (response.status == .ok) {
std.debug.print("Stored Input:\n{s}\n", .{response_body.items});
} else {
std.debug.print("HTTP Error Status: {}\n", .{response.status});
}
}
For some reason the leak only happens when I am trying to use the arraylist but the error occurs in client.deinit() .
From lldb I get TlsInitializationFailed if it is any help .
here is the error I get
error(DebugAllocator): memory address 0x7f9d2f061000 leaked:
/home/usr/Applications/zig-0.16.0/lib/std/array_list.zig:1235:56: 0x15b68f7 in ensureTotalCapacityPrecise (std.zig)
const new_memory = try gpa.alignedAlloc(T, alignment, new_capacity);
^
/home/usr/Applications/zig-0.16.0/lib/std/array_list.zig:606:48: 0x15b9907 in initCapacity (std.zig)
try self.ensureTotalCapacityPrecise(gpa, num);
^
/home/usr/Documents/zig/no1/src/main.zig:15:59: 0x11fa368 in main (main.zig)
var response_body = try std.ArrayList(u8).initCapacity(gpa, 4096);
^
/home/rmashrafi/Applications/zig-0.16.0/lib/std/start.zig:737:30: 0x11fb29e in callMain (std.zig)
return wrapMain(root.main(.{
^
/home/usr/Applications/zig-0.16.0/lib/std/start.zig:190:5: 0x11fa0e1 in _start (std.zig)
asm volatile (switch (native_arch) {
^