Skip to content

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

Closed
@brson

Description

@brson

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

Metadata

Metadata

Assignees

Labels

A-codegenArea: Code generationA-destructorsArea: Destructors (`Drop`, …)P-mediumMedium priority

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions