15

How does Rust handle the "island of isolation" scenario for Rcs and Arcs?

An "island of isolation" is a situation where object A contains a pointer to object B and object B contains a pointer to object A, but there are no pointers to either objects anywhere else.

Is Rust smart enough to detect this or does it lead to memory leaks?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Ayman Madkour
  • 325
  • 1
  • 7
  • 1
    Who calls a reference cycle an "island of isolation"? – trent Dec 29 '18 at 03:10
  • 2
    Seems that this is not about a simply reference cycle, but about reference cycle _without external references_ - isolated from the rest of program. The reference cycle by itself isn't bad, it's bad when it gets lost. – Cerberus Dec 29 '18 at 08:35
  • That was not purely a rhetorical question, by the way - I really do want to know where the term "island of isolation" comes from (or if you just made it up) – trent Dec 30 '18 at 02:29
  • @trentcl It seems to be a commonly used term. For instance, check out this question: https://stackoverflow.com/questions/792831/island-of-isolation-of-garbage-collection – Ayman Madkour Dec 30 '18 at 23:10

1 Answers1

12

Rust does not have a garbage collector, and it won't detect reference cycles. If your program creates inaccessible reference cycles, they are leaked, and it is up to you to avoid them, e.g. by using weak references, or by not using shared ownership in the first place.

Note that the only way to create a reference cycle is to use both shared ownership and interior mutability.

See also the chapter on reference cycles in the Rust book.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841