#need help dealing with a memory leak

1 messages · Page 1 of 1 (latest)

slate heath
#

I'm working on an ECS system scheduler but currently have a memory leak, the output that says so says

C:\Users\{name}\AppData\Roaming\Code\User\globalStorage\ziglang.vscode-zig\zig_install\lib\std\array_list.zig:457:67: 0xf43811 in ensureTotalCapacityPrecise (Mordred-Engine.exe.obj)
                const new_memory = try self.allocator.alignedAlloc(T, alignment, new_capacity);
                                                                  ^
C:\Users\{name}\AppData\Roaming\Code\User\globalStorage\ziglang.vscode-zig\zig_install\lib\std\array_list.zig:434:51: 0xf36960 in ensureTotalCapacity (Mordred-Engine.exe.obj)
            return self.ensureTotalCapacityPrecise(better_capacity);
                                                  ^
C:\Users\{name}\AppData\Roaming\Code\User\globalStorage\ziglang.vscode-zig\zig_install\lib\std\array_list.zig:483:41: 0xf298e1 in addOne (Mordred-Engine.exe.obj)
            try self.ensureTotalCapacity(newlen);
                                        ^
C:\Users\{name}\AppData\Roaming\Code\User\globalStorage\ziglang.vscode-zig\zig_install\lib\std\array_list.zig:262:49: 0xf1affe in append (Mordred-Engine.exe.obj)
            const new_item_ptr = try self.addOne();
                                                ^
C:\Users\{name}\OneDrive\Documents\Mordred-Engine\src\engine\Logic\ECS\ECS_Systems.zig:17:28: 0xee30b5 in addSystem (Mordred-Engine.exe.obj)
    schedule.systems.append(system) catch |err| {
                           ^
C:\Users\{name}\OneDrive\Documents\Mordred-Engine\src\engine\Logic\Engine.zig:12:22: 0xee1d71 in Init (Mordred-Engine.exe.obj)
    systems.addSystem(&schedules.Start, &Start);```

and most of the code responsible is
`engine.zig`
```const std: type = @import("std");
const ecs = @import("./ECS/ECS.zig");
const world = @import("./ECS/ECS_World.zig");
const schedules = @import("../Data/Engine_Schedules.zig");
const systems = @import("../Logic/ECS/ECS_Systems.zig");
const window: type = @import("Renderer/Logic/Renderer_Window.zig");
const allocator = @import("../Logic/Memory/AllocatorManager.zig");

pub fn Init() void {
    allocator.AllocatorManager_init();
    defer allocator.AllocatorManager_deinit();
    systems.addSystem(&schedules.Start, &Start);
}

pub fn Run() void {
    defer systems.clearSchedule(schedules.Start);
    defer systems.clearSchedule(schedules.Update);
    defer systems.clearSchedule(schedules.FixedUpdate);
    defer systems.clearSchedule(schedules.PostUpdate);
    defer systems.clearSchedule(schedules.InputUpdate);
    systems.Progress(&schedules.Start);
}

fn Start() void {
    std.debug.print("Start function\n", .{});
    window.createWindow("Mordred Engine", 1280, 720);
}

fn cleanUp() void {}
#

systems.zig

const schedules = @import("../../Data/Engine_Schedules.zig");
const allocatorManager = @import("../Memory/AllocatorManager.zig");

pub const Schedule = struct {
    systems: std.ArrayList(*const fn () void),
};

pub fn NewSchedule() Schedule {
    const allocator = allocatorManager.getAllocator();
    return Schedule{
        .systems = std.ArrayList(*const fn () void).init(allocator),
    };
}

pub fn addSystem(schedule: *Schedule, system: *const fn () void) void {
    schedule.systems.append(system) catch |err| {
        std.debug.print("ERROR: {}\n", .{err});
        schedule.systems.deinit();
    };
}
pub fn Progress(schedule: *Schedule) void {
    const length = schedule.systems.items.len;
    var i: u32 = 0;
    while (i < length) {
        const func = schedule.systems.items[i];
        func();
        i += 1;
    }
}

pub fn clearSchedule(schedule: Schedule) void {
    schedule.systems.deinit();
}

AllocatorManager.zig


pub const AllocatorManager = struct {
    gpa: std.heap.GeneralPurposeAllocator(.{}),
};

// AllocatorManager functions
pub fn AllocatorManager_init() void {
    const instance = AllocatorManager_getInstance();
    instance.gpa = std.heap.GeneralPurposeAllocator(.{}){};
}

pub fn AllocatorManager_deinit() void {
    const instance = AllocatorManager_getInstance();
    _ = instance.gpa.deinit();
}

pub fn getAllocator() std.mem.Allocator {
    return AllocatorManager_getInstance().gpa.allocator();
}

fn AllocatorManager_getInstance() *AllocatorManager {
    return &globalInstance;
}

// Global AllocatorManager instance
var globalInstance: AllocatorManager = undefined;

and finally the schedules file


pub var Start = systems.NewSchedule();

pub const PreUpdate = systems.NewSchedule();

pub const Update = systems.NewSchedule();

pub const PostUpdate = systems.NewSchedule();

pub const FixedUpdate = systems.NewSchedule();

pub const InputUpdate = systems.NewSchedule();