fixed all memory leaks with GPA

This commit is contained in:
peterino2 2025-07-09 22:16:08 -07:00
parent 943a325ec0
commit 4e93940c12
10 changed files with 60 additions and 12 deletions

View File

@ -31,6 +31,7 @@ pub const LogBuffer = struct {
pub fn deinit(self: *@This()) void {
self.lock.lock();
self.lock.unlock();
self.buffer.deinit();
}
};
@ -266,13 +267,13 @@ pub const LoggerSys = struct {
pub fn print(self: *@This(), comptime fmt: []const u8, args: anytype) !void {
self.lock.lock();
try self.writeOutBuffer.writer().print(fmt, args);
if (self.sessionBuffer != null) {
try self.sessionBuffer.?.lockWriter().print(fmt, args);
self.sessionBuffer.?.unlock();
}
self.lock.unlock();
if (self.sessionBuffer != null) {
// self.sessionBuffer.?.lockWriter().print(fmt, args) catch {};
// self.sessionBuffer.?.unlock();
}
if (self.writeOutBuffer.items.len > LogBufferSize) {
if (self.flushBuffer.items.len == 0) {
try self.flush();

View File

@ -74,6 +74,10 @@ pub fn optimizeBroadPhase() void {
pub const context = core.EngineObject(runtime.PhysicsRuntime).get;
pub fn removeShape(name: *core.Name) void {
context().removeShape(name);
}
pub fn addShape(name: []const u8, settings: ShapeSettings) !void {
var n = core.MakeName(name);
try context().createShape(&n, settings);
@ -85,7 +89,6 @@ pub fn start_module(spec: *core.SpecVariantMap, args: anytype, allocator: std.me
core.engine_logs("starting physics");
try core.defineComponentList(ComponentList, allocator);
try zphysics.init(allocator, .{});
_ = try core.createObject(runtime.PhysicsRuntime, .{ .can_tick = true });
}
@ -96,7 +99,7 @@ pub fn setupFromModule() void {
pub fn shutdown_module(allocator: std.mem.Allocator) void {
core.undefineComponentList(ComponentList);
_ = allocator;
zphysics.deinit();
// core.getEngineObject(runtime.PhysicsRuntime).?.system.destroy();
}
pub const Module: core.ModuleDescription = .{

View File

@ -154,6 +154,7 @@ pub const PhysicsRuntime = struct {
pub fn init(allocator: std.mem.Allocator) !*@This() {
const self = try allocator.create(@This());
try zphysics.init(std.heap.c_allocator, .{});
self.* = .{
.allocator = allocator,
@ -232,22 +233,43 @@ pub const PhysicsRuntime = struct {
self.bodyIdsToDestroy.append(self.allocator, bodyId) catch unreachable;
}
pub fn removeShape(self: *@This(), name: *core.Name) void {
self.shapes.get(name.handle()).?.shape.release();
}
pub fn deinit(self: *@This()) void {
const allocator = self.allocator;
for (PhysicsCharacter.BaseContainer.list.items) |physChar| {
physChar.deinit();
}
var iterator = self.shapes.iterator();
while (iterator.next()) |n| {
n.value_ptr.shape.release();
{
// var iterator = self.shapes.iterator();
// while (iterator.next()) |n| {
// n.value_ptr.shape.release();
// }
}
{
const interface = self.system.getBodyInterfaceMut();
var iterator = self.idToEntity.keyIterator();
while (iterator.next()) |bodyId| {
interface.removeAndDestroyBody(bodyId.*);
}
}
self.bodyIdsToDestroy.deinit(self.allocator);
self.system.update(0.01, .{}) catch {};
self.idToEntity.deinit(self.allocator);
// core.undefineComponent(PhysicsCharacter);
// core.undefineComponent(PhysicsCollider);
self.shapes.deinit(self.allocator);
self.system.destroy();
zphysics.deinit();
allocator.destroy(self);
}

View File

@ -195,6 +195,11 @@ pub const PlatformInstance = struct {
sdl3.getMouseState(&self.cursorPos.x, &self.cursorPos.y);
}
if (event.type == sdl3.events.window_resized) {
self.extent.x = @floatFromInt(event.window.data1);
self.extent.y = @floatFromInt(event.window.data2);
}
for (self.processFuncs.items) |func| {
func(@ptrCast(event));
}

View File

@ -78,8 +78,6 @@ pub const MeshPool = struct {
self.meshUpdates.lock();
defer self.meshUpdates.unlock();
self.destroyList = std.ArrayList(*gpu.GPUTransferBuffer).init(self.allocator);
while (self.meshUpdates.popFromUnlocked()) |u| {
switch (u) {
.new => |new| {
@ -162,6 +160,7 @@ pub const MeshPool = struct {
pub fn destroy(p: *anyopaque) void {
const self: *@This() = @ptrCast(@alignCast(p));
core.engine_logs("mesh pool destroyed");
self.clearInvalidations();
self.invalidations.deinit(self.allocator);

View File

@ -100,6 +100,7 @@ pub fn setup(self: *@This()) !void {
pub fn destroy(self: *@This()) void {
self.screenContext.destroy();
self.stringArena.deinit();
self.runtime.destroy();
self.drawList.deinit();
self.allocator.destroy(self);

View File

@ -10,6 +10,7 @@ pub const ColliderSpec = struct {
pub const TBMap = struct {
root: core.Entity,
shapes: std.ArrayListUnmanaged(core.Name) = .{},
meshes: std.ArrayListUnmanaged(core.Entity) = .{}, // each material has its own entity
colliders: std.ArrayListUnmanaged(core.Entity) = .{}, // each solid in worldspawn has it's own convex collider
colliderSpecs: std.ArrayListUnmanaged(ColliderSpec) = .{},
@ -94,6 +95,7 @@ pub const TBMap = struct {
const shapeName = try std.fmt.bufPrint(&nameBuffer, "t_map/worldspawn_shape_{d}", .{self.colliderSpecs.items.len});
try physics.addShape(shapeName, .{ .convexHull = settings });
try self.shapes.append(self.allocator, core.MakeName(shapeName));
try self.colliderSpecs.append(self.allocator, .{
.name = core.MakeName(shapeName),
@ -132,6 +134,11 @@ pub const TBMap = struct {
}
self.colliderSpecs.deinit(self.allocator);
for (self.shapes.items) |*shapeName| {
physics.removeShape(shapeName);
}
self.shapes.deinit(self.allocator);
self.root.destroy();
self.allocator.destroy(self);

View File

@ -50,8 +50,13 @@ BodyManager::~BodyManager()
// Destroy any bodies that are still alive
for (Body *b : mBodies)
{
if (sIsValidBodyPointer(b))
{
sDeleteBody(b);
}
}
delete [] mActiveBodies;
}

View File

@ -234,6 +234,9 @@ pub const ExternGameObject = struct {
}
pub fn destroy(self: *@This()) void {
if (self.tbMap) |map| {
map.destroy();
}
self.consoleWindow.deinit();
self.physicsObjectsWindow.destroy();
self.allocator.destroy(Slack.fromPtr(self));

View File

@ -415,6 +415,7 @@ pub fn deinit(self: *@This()) void {
// self.rendererDebugger.destroy();
DoomPlayer.DoomCanvas.cleanupDoom();
self.fpcamera.destroy();
self.engineTool.destroy();
// self.objectSpawner.destroy();
// self.videoplayer.destroy();
@ -423,6 +424,7 @@ pub fn deinit(self: *@This()) void {
pub fn main() anyerror!void {
var spec = try backlog.getSpec("sampleGame");
// try spec.put("useGPA", .{ .boolean = false });
_ = backlog.startEngine(&NeonObjectTable, &spec);
// try backlog.initializeAndRunStandardProgram(@This(), .{