Skip to content

Commit

Permalink
Make some further edits to the changes in #1886
Browse files Browse the repository at this point in the history
  • Loading branch information
carols10cents committed Dec 10, 2020
1 parent ea49874 commit d28183f
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/ch15-06-reference-cycles.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

Expand Down

0 comments on commit d28183f

Please sign in to comment.