Skip to content
This repository

Overloaded assignment operators can leak memory #2581

Closed
eholk opened this Issue · 1 comment

1 participant

Eric Holk
Eric Holk
Collaborator
eholk commented

It seems to involve pointers... Here's a test case with several examples and non-examples.

impl methods<T: copy> for [T] {
    fn -(x: [T]/&) -> [T] {
        [x[0], x[0], x[0]]
    }

    fn foo(x: [T]/&) -> [T] {
        [x[0], x[0], x[0]]
    }
}

impl methods<T: copy> for ~T {
    fn +(rhs: ~T) -> ~T {
        rhs
    }
}

impl methods for ~int {
    fn -(rhs: ~int) -> ~int {
        ~(*self - *rhs)
    }
}

fn main() {
    // leaks
    let mut bar = [1, 2, 3];
    bar -= [3, 2, 1];
    bar -= [4, 5, 6];

    io::println(#fmt("%?", bar));

    // okay
    let mut bar = [1, 2, 3];
    bar = bar.foo([3, 2, 1]);
    bar = bar.foo([4, 5, 6]);

    io::println(#fmt("%?", bar));

    // okay
    let mut bar = [1, 2, 3];
    bar = bar - [3, 2, 1];
    bar = bar - [4, 5, 6];

    io::println(#fmt("%?", bar));

    // Leaks
    let mut bar = ~1;
    bar += ~2;
    bar += ~3;

    io:: println(#fmt("%?", bar));

    // Leaks
    let mut bar = ~1;
    bar -= ~2;
    bar -= ~3;

    io:: println(#fmt("%?", bar));
}
Eric Holk
Collaborator
eholk commented

This problem is also present with @int, but they show up as an unreclaimed object in the cycle collector.

Eric Holk eholk referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
Eric Holk eholk referenced this issue from a commit in eholk/rust
Eric Holk Generate a temporary for assign_ops. Issue #2581 fa7db7b
Eric Holk eholk referenced this issue from a commit in eholk/rust
Eric Holk Generate a temporary for assign_ops. Issue #2581 3391b31
Eric Holk eholk referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
Eric Holk eholk referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
Eric Holk eholk closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.