2012-07-11 47 views
2

得到的誤差:匿名函數泄漏內存

$ 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: [email protected]() -> uint { closedUint } } 
} 

#[test] 
fn testForLeakage() { 
    let aFn: boxedFn = createClosure(10); 

    let myInt: uint = aFn.theFn(); 

    assert myInt == 10; 
} 

這是爲什麼泄漏內存?

回答

3

只要你看到內存泄漏,它就是Rust中的一個錯誤(除非你使用本地代碼......在這種情況下,我們的泄漏檢測器可能找不到它)。在這種情況下,問題#1896。

0

上述代碼中的主要錯誤是記錄不能無約束的函數。通過將類型從fn切換到fn @,上面的代碼片段可以工作。