Skip to content

Segmentation fault due to iterator living longer than its parent #13929

Closed
@hatahet

Description

@hatahet

This bug was originally reported on the Rust subreddit by user double_to_bool_conv: http://www.reddit.com/r/rust/comments/24kfmv/iterator_can_live_longer_than_its_parent_is_this/

The version below has been updated after the removal of the ~"string" syntax.

$ rustc -v
rustc 0.11-pre (4f1b0b5 2014-05-03 12:21:47 -0700)
host: x86_64-unknown-linux-gnu

n owned_iterator(slice: &[~str]) -> ~Iterator<~str> {
    ~slice.iter().map(|s| s.clone()) as ~Iterator<~str>
}

fn main() {
    let mut fail = {
        owned_iterator(~["ok".to_owned()])
    };
    let v = ~["fail".to_owned()];
    for i in fail {
        println!("{}", i); // prints "fail"
    }

    let mut segFault = {
        owned_iterator(~["ok".to_owned()])
    };
    let v = ~[-1];
    for i in segFault {
        println!("{}", i); // Segmentation fault 
    }
}

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