Skip to content

Fix one-time memory leak of $MaxExtraPrecision init constant#28

Closed
msollami wants to merge 1 commit into
mainfrom
fix/maxextraprecision-init-leak
Closed

Fix one-time memory leak of $MaxExtraPrecision init constant#28
msollami wants to merge 1 commit into
mainfrom
fix/maxextraprecision-init-leak

Conversation

@msollami

Copy link
Copy Markdown
Collaborator

Summary

system_constants_init leaked a single 64-byte Expr (the $MaxExtraPrecision real) at startup. Reported by macOS leaks and valgrind, rooted at system_constants_init -> expr_new_real. Non-growing, but a genuine one-time leak.

Root cause

add_rule inc-refs (expr_copy) its replacement, so a caller of symtab_add_own_value retains ownership of the value it passes and must free its own reference. register_system_constant does this correctly, but the inlined $MaxExtraPrecision block (which must stay non-Protected, hence inlined) freed only sym, never the expr_new_real(50.0) temporary.

Changes

  • src/core.c: capture the value in val and expr_free(val) after symtab_add_own_value, mirroring register_system_constant.
  • docs/spec/changelog/2026-07-20.md: note the fix.

Testing

  • macOS leaks --atExit ./trace_tests: 0 leaks / 0 bytes (was 1 leak / 64 bytes).
  • Clean C99 build (-std=c99 -Wall -Wextra).

Ticket

beads-planning-dbl

system_constants_init registered $MaxExtraPrecision via
symtab_add_own_value but never freed the expr_new_real(50.0) temporary.
add_rule inc-refs (expr_copy) its replacement, so the caller retains
ownership of its own reference and must free it -- as
register_system_constant already does for the other constants. Capture
the value in a temp and free it, eliminating the single 64-byte Real
leak reported by macOS leaks/valgrind at startup.
stblake added a commit that referenced this pull request Jul 22, 2026
The inline $MaxExtraPrecision registration block in system_constants_init
passed an expr_new_real(50.0) temporary to symtab_add_own_value without
freeing it — a 64-byte EXPR_REAL leak at startup, since
symtab_add_own_value deep-copies its replacement and the caller retains
ownership of its own reference. Capture the value in a local and free it,
mirroring the register_system_constant helper.

macOS `leaks` on trace_tests now reports 0 leaks (was 1 leak / 64 bytes
rooted at system_constants_init -> expr_new_real).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@stblake

stblake commented Jul 22, 2026

Copy link
Copy Markdown
Owner

Landed on main by content in commit c5512a3 (applied directly, since the feature branch this PR targeted had already been merged and main moved ahead). The $MaxExtraPrecision init temporary is now freed and the changelog note is in place. Thanks!

@stblake stblake closed this Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants