mirror of https://github.com/rust-lang/book
Make some further edits to the changes in #1886
This commit is contained in:
parent
ea49874970
commit
d28183f901
|
@ -64,15 +64,16 @@ moment, we’ll get this output:
|
|||
{{#include ../listings/ch15-smart-pointers/listing-15-26/output.txt}}
|
||||
```
|
||||
|
||||
The reference count of the `Rc<List>` instances in both `a` and `b` are 2
|
||||
after we change the list in `a` to point to `b`. At the end of `main`, Rust
|
||||
drops `b` which decreases the reference count of the `b` `Rc<List>` instance
|
||||
by 1. However, the `b` `Rc<List>` can not be collected, because its reference
|
||||
count is 1, not 0. Then Rust drops `a` which decreases the reference count of
|
||||
the `a` `Rc<List>` instance by 1. This instance can not be collected either,
|
||||
because its count is 1, since the uncollected `b` `Rc<List>` instance still
|
||||
refers to it. The memory allocated to the list will remain uncollected forever.
|
||||
To visualize this reference cycle, we’ve created a diagram in Figure 15-4.
|
||||
The reference count of the `Rc<List>` instances in both `a` and `b` are 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 `Rc<List>` instance
|
||||
from 2 to 1. The memory that `Rc<List>` has on the heap won’t be dropped at
|
||||
this point, because its reference count is 1, not 0. Then Rust drops `a`, which
|
||||
decreases the reference count of the `a` `Rc<List>` instance from 2 to 1 as
|
||||
well. This instance's memory can’t be dropped either, because the other
|
||||
`Rc<List>` instance still refers to it. The memory allocated to the list will
|
||||
remain uncollected forever. To visualize this reference cycle, we’ve created a
|
||||
diagram in Figure 15-4.
|
||||
|
||||
<img alt="Reference cycle of lists" src="img/trpl15-04.svg" class="center" />
|
||||
|
||||
|
|
Loading…
Reference in New Issue