← 返回起始笔记位置

知识卡片

普通读书笔记卡 · 758.a.1

共享计数需要处理强引用环

理解等级:中级

Rc让多个拥有者共享同一对象,最后一个强引用释放时析构对象。若对象间形成保留的强引用环,计数无法归零,就会泄漏。Weak不延长对象寿命,访问前需upgrade,且可能失败;把不应承担拥有责任的反向关系设为弱引用可打破部分环。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 1. Types Smart Pointer Types · 查看原书依据

      Rc<T>, which is a reference-counted pointer to an item (roughly analogous to C++’s std::shared_ptr<T>). It implements all of the pointer-related traits and so acts like a Box<T> in many ways. This is useful for data structures where the same item can be reached in different ways, but it removes one of Rust’s core rules around ownership—that each item has onl

    参考来源

    - 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第1章《Smart Pointer Types》(spine5;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch01.html;u046-ch01-064)。 - 结论依据:原文在“Rc<T>”附近的定义、例子或代码支撑本卡核心说明:Rc让多个拥有者共享同一对象,最后一个强引用释放时析构对象。边界按已读前提解释;示例未执行。 - 原始内容:Rc<T>, which is a reference-counted pointer to an item (roughly analogous to C++’s std::shared_ptr<T>). It implements all of the pointer-related traits and so acts like a Box<T> in many ways. This is useful for data structures where the same item can be reached in different ways, but it removes one of Rust’s core rules around ownership—that each item has onl