1 use std::cell::RefCell; 2 use std::rc::Rc; 3 4 #[derive(Clone)] 5 struct Cycle { 6 cell: RefCell<Option<Rc<Cycle>>>, 7 } 8 9 impl Drop for Cycle { drop(&mut self)10 fn drop(&mut self) { 11 println!("freed"); 12 } 13 } 14 15 #[tokio::main] main()16async fn main() { 17 let cycle = Rc::new(Cycle { 18 cell: RefCell::new(None), 19 }); 20 *cycle.cell.borrow_mut() = Some(cycle.clone()); 21 } 22 23 // use nightly rust 24 // RUSTFLAGS="-Z sanitizer=leak" cargo build --example rc-cycle 25 // ./target/debug/example/rc-cycle 26 // ================================================================= 27 // ==1457719==ERROR: LeakSanitizer: detected memory leaks 28 // 29 // Direct leak of 32 byte(s) in 1 object(s) allocated from: 30 // #0 0x55d4688e1b58 in malloc /rustc/llvm/src/llvm-project/compiler-rt/lib/lsan/lsan_interceptors.cpp:56:3 31 // #1 0x55d4689db6cb in alloc::alloc::alloc::h1ab42fe6949393de /rustc/e269e6bf47f40c9046cd44ab787881d700099252/library/alloc/src/alloc.rs:86:14 32 // 33 // SUMMARY: LeakSanitizer: 32 byte(s) leaked in 1 allocation(s). 34