Skip to content

std.regex: every Regex compile leaks the pcre2_maketables tables and a 256-byte block (pre-existing, inherited from the shim) #1036

Closed

Description

@ehartford

Debug-allocator evidence (2026-09-03, darwin-arm64), a program that compiles four patterns (Regex.compile("(") failing, then three succeeding compiles plus a clone) and runs captures/replace/split:

debug-alloc: LEAK addr=… size=2048 origin=with_alloc
debug-alloc: LEAK addr=… size=256 origin=with_alloc
(× 4 pairs)
debug-alloc: leak count=8

Identical verdict on the shim-era std.regex (with_regex_compile in rt/regex_runtime.w) and on the C4 facade (lib/std/regex.w calling pcre2 directly), so the leak predates batch C4 — it is one pair per compile, including a failed one.

The 2,048-byte block is the character tables pcre2_maketables_8(gcontext) builds per compile (regex_compile_code sets ccontext.tables to them; the shim did the same). pcre2 keeps the pointer in the compiled code (re->tables), so the tables must outlive the pattern and be released with pcre2_maketables_free_8 when the Regex drops — today nothing frees them, and Regex has no field to carry them (its ten fields are pinned by CodegenDispatch.gen_regex_literal_value). The 256-byte block per compile is not yet attributed (a tools/debug_drop.w site run will name it).

Two candidate fixes: (a) stop calling pcre2_maketables_8 — the default compile context already carries _pcre2_default_tables_8, and in the "C" locale (With never calls setlocale) maketables produces the same tables, so the call buys nothing; or (b) carry the tables in Regex and free them in drop (touches the literal's codegen layout).

Repro: build any std.regex user with --debug-alloc; e.g. the facade test in the C4 handoff (scratchpad/lib/std/refacade_test.w) or test/behavior/behav_regex_std.w.

Activity

  1. ehartford commented on Oct 3, 2026

    @ehartford
    ContributorAuthor

    Still open for regex literals (found during #2049). A compiled Regex now releases its pair: test/debug_alloc/da_regex_owned_lifetimes.w passes. A literal does not. CodegenDispatch.gen_regex_literal_value caches the compiled code in a per-site global (__regex_lit_<node>, filled once by Regex.__literal_code), and nothing releases it at exit. So every literal site that runs leaves its 256-byte and 2048-byte blocks live.

    Repro, on main 01ec974 plus the #2049 branch:

    use std.regex
    fn main:
        if "zzz" =~ /(\d)/:
            print($1)
        else:
            print("miss")
    

    run --debug-alloc prints two LEAK lines (sizes 256 and 2048) and leak count=2. --debug-alloc-filter=non-root gives the same count: the cache is not marked as a process-lifetime root (with_debug_alloc_mark_root). With three literal sites, behav_regex_capture_fstring and behav_regex_language_semantics show exactly 3 x 2 such leaks once #2049's Captures/binding leaks are fixed.

    Fix: release the cached code at exit, or mark it as a root.

  2. added 2 commits that reference this issue on Oct 3, 2026
  3. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): does not reproduce for literals or compiled patterns.

    $ cat main.w
    use std.regex
    fn main:
        if "zzz" =~ /(\d)/:
            print($1)
        else:
            print("miss")
    $ with run --debug-alloc main.w
    miss
    debug-alloc: leak count=0 allocations=12
    $ with run --debug-alloc compiled.w   # a failing compile, three compiles, three clones, is_match
    4
    debug-alloc: leak count=0 allocations=60
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions