什么是Rc依赖循环的最小示例?

时间:2015-12-26 08:03:03

标签: memory-leaks rust valgrind

我正在尝试编写泄漏内存due to cycles with reference counts的Rust程序。根据Valgrind的说法,以下示例似乎应该导致内存泄漏,并不会泄漏内存。是什么给了什么?

test.rs

use std::cell::RefCell;
use std::rc::Rc;

struct Foo {
    f: Rc<Bar>,
}

struct Bar {
    b: RefCell<Option<Rc<Foo>>>,
}

fn main() {
    let bar = Rc::new(Bar {
        b: RefCell::new(None),
    });
    let foo = Rc::new(Foo { f: bar.clone() });
    *bar.b.borrow_mut() = Some(foo.clone());
}

Valgrind输出:

$ rustc --version
rustc 1.4.0 (8ab8581f6 2015-10-27)
$ rustc -o test test.rs
$ valgrind test
==23331== Memcheck, a memory error detector
==23331== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==23331== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==23331== Command: test
==23331== 
==23331== 
==23331== HEAP SUMMARY:
==23331==     in use at exit: 0 bytes in 0 blocks
==23331==   total heap usage: 37 allocs, 37 frees, 9,078 bytes allocated
==23331== 
==23331== All heap blocks were freed -- no leaks are possible
==23331== 
==23331== For counts of detected and suppressed errors, rerun with: -v
==23331== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

1 个答案:

答案 0 :(得分:1)

使用您正在使用的Rust版本时,您最有可能使用的是jemalloc,它并不总能与Valgrind配合使用(有关详细信息,请参阅链接的问题)。对于Rust的现代版本,默认情况下使用系统分配器,并且您发布的代码会报告内存泄漏:

$ valgrind --leak-check=full ./test
==761== Memcheck, a memory error detector
==761== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==761== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==761== Command: ./test
==761==
==761==
==761== HEAP SUMMARY:
==761==     in use at exit: 56 bytes in 2 blocks
==761==   total heap usage: 13 allocs, 11 frees, 2,233 bytes allocated
==761==
==761== 56 (32 direct, 24 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2
==761==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==761==    by 0x10BDAB: alloc::alloc::alloc (in /tmp/test)
==761==    by 0x10BD17: alloc::alloc::exchange_malloc (in /tmp/test)
==761==    by 0x10C3F6: <alloc::rc::Rc<T>>::new (in /tmp/test)
==761==    by 0x10BF6F: test::main (in /tmp/test)
==761==    by 0x10DAF2: std::rt::lang_start::{{closure}} (in /tmp/test)
==761==    by 0x115CC2: {{closure}} (rt.rs:49)
==761==    by 0x115CC2: std::panicking::try::do_call (panicking.rs:297)
==761==    by 0x117BA9: __rust_maybe_catch_panic (lib.rs:87)
==761==    by 0x11677C: try<i32,closure> (panicking.rs:276)
==761==    by 0x11677C: catch_unwind<closure,i32> (panic.rs:388)
==761==    by 0x11677C: std::rt::lang_start_internal (rt.rs:48)
==761==    by 0x10DAD4: std::rt::lang_start (in /tmp/test)
==761==    by 0x10C19A: main (in /tmp/test)
==761==
==761== LEAK SUMMARY:
==761==    definitely lost: 32 bytes in 1 blocks
==761==    indirectly lost: 24 bytes in 1 blocks
==761==      possibly lost: 0 bytes in 0 blocks
==761==    still reachable: 0 bytes in 0 blocks
==761==         suppressed: 0 bytes in 0 blocks
==761==
==761== For counts of detected and suppressed errors, rerun with: -v
==761== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

我在Ubuntu上使用Valgrind 3.13.0和Rust 1.34.1,但我不认为这会改变结果。

您还可以向结构添加虚拟值,以便在输出中更轻松地找到它们。我使用了一个非常出色的Box<[u8; 10240]>

至于 minimal ,我为链表建模:

use std::cell::RefCell;
use std::rc::Rc;

struct Node {
    next: RefCell<Option<Rc<Node>>>,
}

fn main() {
    let foo1 = Rc::new(Node {
        next: RefCell::new(None),
    });
    let foo2 = Rc::new(Node {
        next: RefCell::new(Some(foo1.clone())),
    });
    *foo1.next.borrow_mut() = Some(foo2.clone());
}

该程序还报告泄漏。

另见: