知识卡片
强引用闭环无法归零
当 a 和 b 互相持有 Rc,即使外部的两个所有者都退出,每个值仍有一条来自对方的强引用。两边计数都达不到 0,因此都不会析构。可以把不表达所有权的连接建模为 Weak,避免它维持目标寿命。
相关卡片 [[强计数决定值何时释放]]、[[弱引用升级检查存活]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 智能指针 Reference Cycles Can Leak Memory · 查看原书依据
The reference count of the Rc<List> instances in both a and b is 2 after we change the list in a to point to b. At the end of main, Rust drops the variable b, which decreases the reference count of the b Rc<List> instance from 2 to 1.
参考来源
- 位置:《The Rust Programming Language(第2版)》第15章 智能指针(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c15.xhtml`,块 0309)
- 结论依据:两节点的计数从 2 各降到 1,说明外部句柄落幕不能触发释放。
- 原始内容:The reference count of the Rc<List> instances in both a and b is 2 after we change the list in a to point to b. At the end of main, Rust drops the variable b, which decreases the reference count of the b Rc<List> instance from 2 to 1.