得到的错误:
$ 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)
Rust 代码(减少测试用例):
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;
}
为什么会泄漏内存?
最佳答案
任何时候你看到内存泄漏,这都是 Rust 中的一个错误(除非你正在处理 native 代码......在这种情况下,我们的泄漏检测器可能不会发现它)。在本例中,问题是#1896。
关于memory-leaks - 匿名函数内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11441584/