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

Finalizer not run if struct with dtor is not assigned to a variable. #4734

Closed
brson opened this issue Feb 1, 2013 · 4 comments · Fixed by #13390
Closed

Finalizer not run if struct with dtor is not assigned to a variable. #4734

brson opened this issue Feb 1, 2013 · 4 comments · Fixed by #13390
Assignees
Labels
A-codegen Area: Code generation A-destructors Area: destructors (Drop, ..) P-medium Medium priority
Milestone

Comments

@brson
Copy link
Contributor

brson commented Feb 1, 2013

This leaks. (and it fails to perform all the other side-effects one might expect from relevant destructors.)

use std::cast::transmute;
use std::libc::c_void;

struct Rattle { croak: ~str }

impl Drop for Rattle {
    fn drop(&mut self) {
        println!("Goodbye {}",self.croak);
    }
}

struct NonCopyable {
    r: Rattle,
    p: *c_void
}

impl Drop for NonCopyable {
    fn drop(&mut self) {
        let p = self.p;
        let _v = unsafe { transmute::<*c_void, ~int>(p) };
    }
}

fn main() {
    let t = ~0;
    let p = unsafe { transmute::<~int, *c_void>(t) };

    let _x = NonCopyable { r: Rattle{ croak: ~"assigned" }, p: p };
    // assigning this to a variable makes it not leak
    NonCopyable { r: Rattle{ croak: ~"unassigned" }, p: p };
}

Namely, running it prints:

% rustc /tmp/issue4734.rs && /tmp/issue4734
warning: no debug symbols in executable (-arch x86_64)
Goodbye assigned
@ghost ghost assigned catamorphism Feb 7, 2013
@msullivan
Copy link
Contributor

Confirmed as still a bug. Updated test case in description.

@pnkfelix
Copy link
Member

Visiting for bug triage, email from 2013-08-19

Updated test case with a more visible way to see what's going wrong.

It is probably related to #3511

@ghost ghost assigned nikomatsakis Sep 26, 2013
@alexcrichton
Copy link
Member

#3511 has landed, but this has not been fixed.

@nikomatsakis
Copy link
Contributor

I expect this is due to that goofy "ignore" mode, which I think ought to be removed from trans. In controlflow.rs, there is this fragment:

        ast::StmtExpr(e, _) | ast::StmtSemi(e, _) => {
            bcx = expr::trans_into(cx, e, expr::Ignore);
        }

I am pretty sure that Ignore mode is not running the relevant destructors. At minimum, we should change this code to check whether type_needs_drop and use trans_to_lvalue if so.

alexcrichton added a commit to alexcrichton/rust that referenced this issue Apr 8, 2014
Previously, if statements of the form "Foo;" or "let _ = Foo;" were encountered
where Foo had a destructor, the destructors were not run. This changes
the relevant locations in trans to check for ty::type_needs_drop and invokes
trans_to_lvalue instead of trans_into.

Closes rust-lang#4734
Closes rust-lang#6892
bors added a commit that referenced this issue Apr 16, 2014
Previously, if statements of the form "Foo;" or "let _ = Foo;" were encountered
where Foo had a destructor, the destructors were not run. This changes
the relevant locations in trans to check for ty::type_needs_drop and invokes
trans_to_lvalue instead of trans_into.

Closes #4734
Closes #6892
flip1995 pushed a commit to flip1995/rust that referenced this issue Feb 25, 2021
Rework use_self impl based on ty::Ty comparison rust-lang#3410 | Take 2

This builds on top of rust-lang#5531

I already reviewed and approved the commits by `@montrivo.` So only the review of my commits should be necessary.

I would also appreciate your review `@montrivo,` since you are familiar with the challenges here.

Fixes rust-lang#3410 and Fixes rust-lang#4143 (same problem)
Fixes rust-lang#2843
Fixes rust-lang#3859
Fixes rust-lang#4734 and fixes rust-lang#6221
Fixes rust-lang#4305
Fixes rust-lang#5078 (even at expression level now 🎉)
Fixes rust-lang#3881 and Fixes rust-lang#4887 (same problem)
Fixes rust-lang#3909

Not yet: rust-lang#4140 (test added)

All the credit for the fixes goes to `@montrivo.` I only refactored and copy and pasted his code.

changelog: rewrite [`use_self`] lint and fix multiple (8) FPs. One to go.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation A-destructors Area: destructors (Drop, ..) P-medium Medium priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants