Reference Counting and Object Cycles
Track incoming references so zero-count objects can be reclaimed promptly, then reason about the strongly connected case.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 2
- Reading time
- 1 min
In this article2 chapters
Every owning reference contributes one count
When creating an owning pointer, increment the target’s count. Dropping that pointer decrements it. A zero count can trigger reclamation and release the object’s outgoing references, possibly causing more objects to reach zero. Assignment should protect the new target before dropping the old one when their identities can coincide. Threaded implementations must also coordinate increments with object lifetime and concurrent access.
A disconnected cycle may retain positive counts
If two objects point to one another and all external roots are removed, each still receives an incoming reference from the other. Neither count reaches zero. Weak references, explicit structural restrictions or a cycle collector can address this limitation under different designs. Mark and sweep detects the cycle’s inaccessibility by tracing from roots. Ownership and borrowing distinguishes lifetime-retaining references from borrows that do not own the resource.[1][2]