Memmory Leak

pub fn editorOpen(fliename: [*:0]u8) !void {
    var file = try std.fs.cwd().openFile(std.mem.span(fliename), .{});
    defer file.close();

    var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true }){};
    const allocator = gpa.allocator();
    defer _ = gpa.deinit();

    while (try file.reader().readUntilDelimiterOrEofAlloc(allocator, '\n', std.math.maxInt(usize))) |line| {
        defer allocator.free(line);

        // Trim trailing newline or carriage return characters
        var linelen = line.len;
        while (linelen > 0 and (line[linelen - 1] == '\n' or line[linelen - 1] == '\r')) {
            linelen -= 1;
        }

        // Allocate memory for E.row.chars and copy the line
        // allocator.free(E.row.chars);
        E.row.chars = try allocator.alloc(u8, linelen + 1);
        @memcpy(E.row.chars[0..linelen], line[0..linelen]);
        E.row.chars[linelen] = '0'; // Null-terminate the string
        // defer allocator.free(E.row.chars);

        E.numrows = 1;
    }
}

var ab = std.ArrayListUnmanaged(u8){};
ab.appendSlice(allocator,"~");
ab.appendSlice(allocator,E.row.chars);

i am getting error when trying to read from file line by line and store it in struct like below
what is may be the possible cause of the error can anybody help

error(gpa): memory address 0x7ffbce77f000 leaked:gpa

const erow = struct { size: c_int, chars: []u8 };
 var E: editorConfig = editorConfig{ .cx = 0, .cy = 0, .screenrows = 0, .screencols = 0, .orig_termios = undefined, .row = undefined, .numrows = 0 };

At each iteration, you overwrite the previous slice. I see you’ve tried placing some frees around it, that’s not going to work. You need to free them once you’re done using them.

i have tried adding

 allocator.free(E.row.chars);

after above line now i am getting
Segmentation fault at address 0x7f85c5d42000