Skip to content

Segfault on calling method on owned trait object #7208

Closed

Description

@benw

This segfaults on the second call to fruit.eat():

trait Fruit {
  fn eat(&self);
}

struct Apple {
  x: int
}

impl Fruit for Apple {
  fn eat(&self) {
    println(fmt!("Crunching on apple %d", self.x));
  }
}

fn main() {
  let fruits: ~[ ~Fruit ] = ~[
    ~Apple{ x: 1 } as ~Fruit,
    ~Apple{ x: 2 } as ~Fruit,
    ~Apple{ x: 3 } as ~Fruit
  ];

  for fruits.each |fruit| {
    println(~"before");
    fruit.eat();
    println(~"after");
  }
}

Results:

> rustc --version
rustc 0.6
host: x86_64-apple-darwin
> rustc test.rs 
warning: no debug symbols in executable (-arch x86_64)
> ./test
before
Crunching on apple 1
after
before
Segmentation fault: 11

Activity

  1. thestinger commented on Jun 23, 2013

    @thestinger
    Contributor

    Fixed by #7214.

  2. added a commit that references this issue on Aug 21, 2026
    4cc333c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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