fix: memory leak from snapshot/undo system

ci-dev
LeRoyce Pearson 2024-04-23 22:17:35 -06:00
parent 80caf66884
commit b82fdbca00
1 changed files with 19 additions and 3 deletions

View File

@ -111,6 +111,7 @@ pub fn init(context: *seizer.Context) !void {
} }
fn resetGame() !void { fn resetGame() !void {
resetHistory();
draw_pile.deinit(gpa); draw_pile.deinit(gpa);
draw_pile_exhausted = false; draw_pile_exhausted = false;
drawn_cards.shrinkRetainingCapacity(0); drawn_cards.shrinkRetainingCapacity(0);
@ -151,6 +152,11 @@ fn resetHistory() void {
fn destroy(window: *seizer.Window) void { fn destroy(window: *seizer.Window) void {
_ = window; _ = window;
for (history.items) |*snapshot| {
snapshot.deinit();
}
history.deinit(gpa);
draw_pile.deinit(gpa); draw_pile.deinit(gpa);
drawn_cards.deinit(gpa); drawn_cards.deinit(gpa);
for (&stacks) |*stack| { for (&stacks) |*stack| {
@ -454,7 +460,7 @@ pub fn doSelectOrPlace(pressed: bool) !void {
} }
} }
} else if (hovered_deck == selected_deck) { } else if (hovered_deck == selected_deck) {
const snapshot = try Snapshot.initFromCurrentGlobalState(); var snapshot = try Snapshot.initFromCurrentGlobalState();
const selected_substack = selected_deck.?.items[selected_card..]; const selected_substack = selected_deck.?.items[selected_card..];
// try to move all selected cards into the foundations // try to move all selected cards into the foundations
@ -488,12 +494,14 @@ pub fn doSelectOrPlace(pressed: bool) !void {
if (cards_moved) { if (cards_moved) {
try history.append(gpa, snapshot); try history.append(gpa, snapshot);
} else {
snapshot.deinit();
} }
selected_deck = null; selected_deck = null;
} else { } else {
var cards_moved = false; var cards_moved = false;
const snapshot = try Snapshot.initFromCurrentGlobalState(); var snapshot = try Snapshot.initFromCurrentGlobalState();
if (selected_deck) |selected| move_from_selected_to_hovered: { if (selected_deck) |selected| move_from_selected_to_hovered: {
const selected_substack = selected.items[selected_card..]; const selected_substack = selected.items[selected_card..];
if (selected_substack.len == 0) break :move_from_selected_to_hovered; if (selected_substack.len == 0) break :move_from_selected_to_hovered;
@ -516,7 +524,7 @@ pub fn doSelectOrPlace(pressed: bool) !void {
hovered.ensureUnusedCapacity(gpa, 1) catch break :move_from_selected_to_hovered; hovered.ensureUnusedCapacity(gpa, 1) catch break :move_from_selected_to_hovered;
hovered.appendAssumeCapacity(selected_deck.?.pop()); hovered.appendAssumeCapacity(selected_deck.?.pop());
hovered_card = indexOfTopOfStack(hovered_deck.?.items); hovered_card = indexOfTopOfStack(hovered_deck.?.items);
try history.append(gpa, snapshot); cards_moved = true;
break :move_from_selected_to_hovered; break :move_from_selected_to_hovered;
} else for (foundations[0..]) |*foundation| { } else for (foundations[0..]) |*foundation| {
if (foundation != selected) continue; if (foundation != selected) continue;
@ -550,6 +558,8 @@ pub fn doSelectOrPlace(pressed: bool) !void {
} }
if (cards_moved) { if (cards_moved) {
try history.append(gpa, snapshot); try history.append(gpa, snapshot);
} else {
snapshot.deinit();
} }
selected_deck = null; selected_deck = null;
} }
@ -778,6 +788,12 @@ const Snapshot = struct {
pub fn deinit(snapshot: *@This()) void { pub fn deinit(snapshot: *@This()) void {
gpa.free(snapshot.draw_pile); gpa.free(snapshot.draw_pile);
gpa.free(snapshot.drawn_cards); gpa.free(snapshot.drawn_cards);
for (snapshot.stacks) |stack| {
gpa.free(stack);
}
for (snapshot.foundations) |foundation| {
gpa.free(foundation);
}
} }
pub fn restore(snapshot: @This()) !void { pub fn restore(snapshot: @This()) !void {