得到的错误:
$ rustc leakyFunction.rs --test
$ ./leakyFunction
running 1 test
test testForLeakage ... Unreclaimed object found at 0xb6d02d98: ((), (10))
leaked memory in rust main loop (1 objects)
leakyFunction: /home/havvy/rust/src/rt/memory_region.cpp:172:
memory_region::~memory_region(): Assertion `false' failed.
Aborted (core dumped)
锈代码(简化的测试用例):
use std;
type boxedFn = { theFn: fn () -> uint };
fn createClosure (closedUint: uint) -> boxedFn {
{ theFn: fn@ () -> uint { closedUint } }
}
#[test]
fn testForLeakage () {
let aFn: boxedFn = createClosure(10);
let myInt: uint = aFn.theFn();
assert myInt == 10;
}
为什么会泄漏内存?