Closed
Description
Here's a bit of a verbose test case for a situation where I get away with accessing a value in a ~
-closure environment after its destructor has run. valgrind also complains. Not sure whether this falls under a known bug, I didn't really know what terms to search for.
struct A {
foo : Option<~fn(&mut A)>
}
impl A {
fn call(&mut self) {
(*self.foo.get_ref())(self); // this is probably the broken bit
}
}
struct S {
msg: ~str
}
impl Drop for S {
fn drop(&mut self) {
println!("drop {:?}", *self);
self.msg = ~"dropped";
}
}
fn main() {
let x = S { msg: ~"ok" };
let f: ~fn(&mut A) = |a| { a.foo = None; println!("{:?}", x); }; // this frees closure environment via `a`, then accesses it
let mut a = A { foo: Some(f) };
a.call();
}
output:
drop S{msg: ~"ok"}
S{msg: ~"dropped"}