Earlier quoted context omitted.
> AFAICT a reference counting scheme must be able to nullify all references to an object when it's deallocated, so deallocations can become arbitrarily expensive The idea of reference-counted object is that by the time it's deallocated there _are_ no references left to update. Now you might reasonably be asking about the memory left behind by those old references which contain now-invalid pointers. The use-after-free…
Well, there's a concept of a _weak_ reference. I don't know whether it exists in Rust but I can't imaigne a modern reference counting system without support for it. A weak reference does not prevent the object from being deallocated, but the system guarantees that when the object is deallocated, all weak references to it will be nullified. This is what makes deallocating objects in a refcounting system potentially ar…
It does:
- https://doc.rust-lang.org/std/rc/struct.Weak.html
- https://doc.rust-lang.org/std/sync/struct.Weak.html
> A weak reference does not prevent the object from being deallocated, but the system guarantees that when the object is deallocated, all weak references to it will be nullified.
> This is what makes deallocating objects in a refcounting system potentially arbitrarily expensive.
Depends how you implement weakrefs. In Rust, when you upgrade a weakref it checks if there are outstanding strong references, and if there are not the control block is considered invalid and the upgrade fails.
There is no need to touch any of the weakrefs at any point.
And unlike languages like Python, Rust does not have weakref finalizers either.
Deallocation is still arbitrarily expensive in general, as the entire subtree will get deallocated recursively, but that has nothing to do with refcounting.