Skip to content

IoError.to_str() leaks five allocations per call #1392

Closed

Description

@ehartford

Found during #1363.

use std.fs
fn main:
    match read_file("/tmp/does-not-exist"):
        Ok(s) => print(s.len())
        Err(e) =>
            let m = e.to_str()
            print(m)

with run --debug-alloc (installed v0.15.2.1, origin/main 8c859a7): debug-alloc: leak count=5 (four 16-byte and one 32-byte blocks). e.message() (lib/std/fs.w, hand-written) is leak count=0, so the leak is in the to_str generated for the pub error IoError declaration (or its Display glue).

Expected: leak count=0.

Activity

  1. ehartford commented on Sep 23, 2026

    @ehartford
    ContributorAuthor

    The same leak appears for any error declaration's generated Display, not only IoError.to_str(). With the seed at 8c859a7:

    error DbError =
        | Failed(status: i32)
    
    fn main:
        let e = DbError.Failed(3)
        print(f"err {e}")
    

    with run --debug-alloc gives leak count=3, and let s = e.display(); print(s) gives the same. Found while writing the stage 5 facade fixtures (#1426), where <R>Error is a rendered error declaration. The fixtures avoid displaying the error until this is fixed.

  2. ehartford commented on Sep 23, 2026

    @ehartford
    ContributorAuthor

    Same leak with a user error declaration and no std involvement (origin/main 1c76062, base stage2), found while writing the #1430 fixtures (behav_1430_error_from_declared_later.w shows it under --debug-alloc, declared in either order):

    error DbError =
        | Failed(code: i32, detail: i64)
        | Missing(what: str)
    
    fn main:
        let e = DbError.Failed(1, 2)
        let s = f"{e}"
        print(s)
    

    with run --debug-alloc: prints Failed(1, 2), debug-alloc: leak count=6 (all 16-byte blocks). With DbError.Missing("users") alone: leak count=2. Wrapping it (error AppError from DbError, f"{AppError.Db(DbError.Failed(1, 2))}") leaks likewise. So the generated Display for any error declaration leaks per formatted field, not just IoError.to_str.

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