mirror of
https://github.com/anomalyco/opentui.git
synced 2026-04-25 13:06:00 +03:00
[PR #431] [MERGED] fix(core): fix memory leaks in Zig unicode encoding and grapheme pool #1312
Labels
No labels
bug
core
documentation
feature
good first issue
help wanted
pull-request
question
react
solid
tmux
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/opentui#1312
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/anomalyco/opentui/pull/431
Author: @edlsh
Created: 12/19/2025
Status: ✅ Merged
Merged: 12/22/2025
Merged by: @kommander
Base:
main← Head:fix/zig-memory-leaks📝 Commits (6)
a000c77fix(core): fix memory leaks in Zig unicode encoding and grapheme poolf2c6782test(core): add regression tests for memory leak fixes294ed80fix(test): use const for unmutated success variable530c1a9chore: remove build artifact7e57a7etest: verify slot cleanup via WrongGeneration check232bc19fix(core): memory leaks, race conditions, and additional tests📊 Changes
10 files changed (+0 additions, -0 deletions)
View changed files
📝
packages/core/src/lib/tree-sitter/client.test.ts(+15 -0)📝
packages/core/src/lib/tree-sitter/client.ts(+2 -0)📝
packages/core/src/zig/buffer.zig(+4 -6)📝
packages/core/src/zig/grapheme.zig(+32 -1)📝
packages/core/src/zig/lib.zig(+30 -0)📝
packages/core/src/zig/renderer.zig(+13 -0)📝
packages/core/src/zig/test.zig(+2 -0)📝
packages/core/src/zig/tests/buffer_test.zig(+20 -0)➕
packages/core/src/zig/tests/memory_leak_regression_test.zig(+159 -0)📝
packages/core/src/zig/text-buffer-view.zig(+3 -0)📄 Description
Summary
Fixes critical memory leaks in the Zig native code for unicode encoding and grapheme pool operations.
Changes
1. Fix
errdeferthat never runs inencodeUnicode(lib.zig)Problem: The function returns
bool, buterrdeferonly triggers on error union returns. All failure paths usedcatch return false, which is a normal return—not an error return—so cleanup never executed.Fix: Replace
errdeferwithdefer+ success flag pattern:var success = false;at starterrdefertodefer { if (!success) { ... } }success = true;before finalreturn true;2. Fix grapheme leak on realloc failure (lib.zig)
Problem: After
pool.alloc()+pool.incref(), ifreallocfails, the newly allocated grapheme wasn't tracked yet in the result array, so it would leak.Fix: Track pending grapheme with
var pending_gid: ?u32 = null;:pending_gid = gidimmediately after allocationpending_gid = nullafter successfully stored in result arraypending_gidif set3. Add bounds checks in GraphemePool (grapheme.zig)
Problem:
incref,decref,get, andgetRefcountdidn't validateclass_idbefore array access.Fix: Added
if (class_id >= MAX_CLASSES) return GraphemePoolError.InvalidId;to all four methods.Testing
bun run buildpasses (all 6 platform binaries)bun run test:nativepasses (1241/1242 tests, 1 skipped)🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.