Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ouroboros leaks memory if a builder panics #31

Closed
Voultapher opened this issue Jun 22, 2021 · 3 comments
Closed

ouroboros leaks memory if a builder panics #31

Voultapher opened this issue Jun 22, 2021 · 3 comments

Comments

@Voultapher
Copy link

Given this program, and ouroboros 0.9:

use ouroboros::self_referencing;

#[self_referencing]
struct StringCell {
    owner: Box<String>,

    #[borrows(owner)]
    dependent: &'this String,
}

fn main() {
    let _ = StringCellBuilder {
        owner: Box::new("some string into".into()),
        dependent_builder: |_| todo!(),
    }
    .build();
}

And running it with miri:

$ cargo +nightly miri run

I get:

thread 'main' panicked at 'not yet implemented', src/main.rs:14:32
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
The following memory was leaked: alloc1433 (Rust heap, size: 16, align: 1) {
73 6f 6d 65 20 73 74 72 69 6e 67 20 69 6e 74 6f │ some string into
}
alloc1503 (Rust heap, size: 24, align: 8) {
0x00 │ ╾alloc1433[<untagged>]╼ 10 00 00 00 00 00 00 00 │ ╾──────╼........
0x10 │ 10 00 00 00 00 00 00 00                         │ ........
}

error: the evaluated program leaked memory

error: aborting due to previous error

Per se leaking is safe to do, but at least you should document it. I looked through the documentation and could not find any mention of it.

This came up in a discussion about self_cell and I've since revised my stance and switched from documenting the possible leak and providing an example that shows how to prevent it with catch_unwind, to handling the leak with a scope guard Voultapher/self_cell@d457300

@someguynamedjosh
Copy link
Owner

Thanks for bringing this up. For now I've added a note to the documentation for 0.9.5, if I have some more time I'll look into how I can use the approach you linked to aovid this behavior altogether.

@someguynamedjosh
Copy link
Owner

This behavior no longer happens with versions after 0.10.0, due to some changes in the constructor logic.

@Ten0
Copy link

Ten0 commented Nov 22, 2022

Looks like the documentation still mentions this as happening 🤔

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

No branches or pull requests

3 participants