Closed
Description
The following program will segfault, double free, leak, and generally misbehave:
type T = {mut f: @int};
fn foo(++x: T) { x.f = @4; }
fn main() {
let x = {mut f: @3};
foo(x);
}
The reason is our by-value protocol: we copy the data for the record but do not invoke the take-glue (nor drop-glue). As a result, the assignment x.f = @4
within foo()
causes the original @3
to be freed, but never arranges for the new @4
to be released. When foo()
returns, main()
tries to drop its copy of x
, which still contains the original @3
pointer.
Of course the same badness would happen if the type T included a unique pointer. Or really anything that requires take/drop-glue.