Fix example crashes: allocator misuse and integer underflow#93
Merged
Conversation
The fixes from PR #92 landed in feature/examples-batch-7 after #91 had already merged into main, so they never reached main. Re-apply against main directly. Two bug classes: 1. Allocator misuse (5 examples). Component state was initialized with ctx.allocator — the per-frame arena that resets every render — instead of ctx.persistent_allocator. After the first render the internal array_lists pointed at freed memory, causing segfaults / bus errors on the second frame: - braille_canvas.zig — BrailleCanvas cells - data_table.zig — DataTable columns/rows - rich_log.zig — RichLog entries + search_term buffer - screen_stack.zig — ScreenStack stack array - action_system.zig — ActionRegistry actions, CommandPalette commands, palette shortcut strings Switched all per-component init to ctx.persistent_allocator, matching the pattern used by examples/file_browser.zig. 2. Integer underflow in async_tasks. Pressing 's' to relaunch while previous tasks were still in-flight reset tasks_launched to 3 but the OLD generation's completion events kept arriving and decremented past zero. Two-part fix: guard against re-launch while tasks are running, and use saturating subtraction (-|=) so any stray duplicate event can't underflow. Verified with stress tests (rapid keystrokes, multi-launch sequences).
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Re-apply the fixes from PR #92, which never reached main. (#92 was based on `feature/examples-batch-7`; that branch was merged into main before #92 was merged into the feature branch, so the fixes ended up orphaned.)
Bug 1 — Allocator misuse (5 examples)
Component state was initialized with `ctx.allocator` — the per-frame arena that resets every render — instead of `ctx.persistent_allocator`. The internal `array_list` storage pointed at freed memory after the first `render()`, segfaulting on the second frame.
Switched all per-component init to `ctx.persistent_allocator`, matching the pattern in `examples/file_browser.zig`. Also moved the palette `shortcut` string allocations to persistent in `action_system` since `CommandPalette` stores command structs by reference.
Bug 2 — `async_tasks` integer underflow
Pressing `s` to relaunch while previous tasks were still in-flight reset `tasks_launched` to 3, but the old generation's completion events kept arriving and decremented `tasks_launched` past zero — `u8` underflow panic on the fourth completion.
Two-part fix:
Test plan