#why am i leaking memory?

1 messages · Page 1 of 1 (latest)

glad veldt
#

this is my code

    const url = try std.fmt.allocPrint(alloc, "{s}/register", .{LOCALHOST});
    defer alloc.free(url);

    var client = http.Client{
        .allocator = alloc,
    };
    defer client.deinit();

    var body = std.ArrayList(u8).init(alloc);
    errdefer body.deinit();

    var result: http.Client.FetchResult = undefined;

    if (method == .GET) {
        result = try client.fetch(.{
            .location = .{ .url = url },
            .method = method,
            .extra_headers = &.{
                .{ .name = "content-type", .value = "application/json" },
            },
            .response_storage = .{ .dynamic = &body },
        });
    } else {
        var string = std.ArrayList(u8).init(alloc);
        defer string.deinit();
        try std.json.stringify(userCreds, .{}, string.writer());

        result = try client.fetch(.{
            .location = .{ .url = url },
            .method = method,
            .payload = string.items,
            .extra_headers = &.{
                .{ .name = "content-type", .value = "application/json" },
            },
            .response_storage = .{ .dynamic = &body },
        });
    }

    return receivedResult{
        .status = result.status,
        .body = try body.toOwnedSlice(),
    };
}```
#

the stack trace is talking about the else statements's client.fetch

modern heron
#

are you freeing the receivedResult.body at the callsite of this function?